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.9964578535956675
0.22962271257843092
0.8882285417902899
0.3847174275762275
0.009809755383860086

Random values in [0,100)
45.4213980892875
30.97896136837037
53.20329636388269
60.86863775719632
94.74025839092673

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

Next: