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.48375121235498675
0.8647968704552128
0.4929263797061755
0.3409602876733201
0.965956707208973

Random values in [0,100)
30.8860091098352
34.34172579057212
62.19079352228785
38.040676777509304
82.31280465257682

Random integers in [1,10]
1
9
8
8
7

Next: