Practical Coding in Python

Learn to write and validate your own code

Darren Kessner, PhD

(revised September 1, 2025)

Previous: hello_math

hello_random

#
# hello_random.py
#

import random

# random() returns a value in [0,1)

print("Random values in [0,1)")
for i in range(5):
    print(random.random())


# uniform(a,b) returns a value in [a,b)

print()
print("Random values in [0,100)")
for i in range(5):
    print(random.uniform(0, 100))


# randint(a,b) returns an integer in [a,b] (note: closed interval)

print()
print("Random integers in [1,10]")
for i in range(5):
    print(random.randint(1, 10))

Output:

Random values in [0,1)
0.5591819042454939
0.7247238545988786
0.6616358263509973
0.642077672121114
0.3913644489649034

Random values in [0,100)
53.6709449001726
43.5007966837268
20.116804345209516
73.17427920697921
5.755961093736206

Random integers in [1,10]
3
10
4
4
3

Next: