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.1977059465278942
0.2685040155911671
0.5848378099031898
0.4560204348531258
0.08421149063589095

Random values in [0,100)
37.90352522667799
7.435515655176095
61.49136697574815
80.30470880293335
84.37998200171114

Random integers in [1,10]
5
10
4
2
5

Next: