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.06608395699629366
0.043073861462160634
0.08865029623883303
0.3549607083680031
0.6079412725785855

Random values in [0,100)
53.89952326527588
83.07898960878896
90.16356787671536
83.04662622580699
94.3858160985982

Random integers in [1,10]
7
2
2
5
8

Next: