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.37891120024509384
0.7992054106919718
0.6116847308279222
0.5248247807777751
0.5419879140345513

Random values in [0,100)
8.729163245291815
7.647632680011096
91.40133898422594
47.37182731514259
63.8652357315827

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

Next: