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.7795288013722138
0.7527434884383645
0.49410886694675393
0.5927454429092718
0.9104072535046999

Random values in [0,100)
55.71087919972187
34.85768859198345
16.34667595031375
55.59871421831332
17.328052785998814

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

Next: