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.585961360088896
0.18310505104513708
0.42122895197406385
0.4586921921900672
0.8552122193463879

Random values in [0,100)
0.12792921124947787
12.627624801719916
61.359384626313584
15.253554451169215
78.34393981946957

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

Next: