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.7648853548657344
0.9928375207010423
0.2247338046996219
0.4034149390323791
0.14565914216919207

Random values in [0,100)
99.84274413317212
68.50254586007097
50.76187480898601
45.96106853644885
69.09078356284081

Random integers in [1,10]
9
5
6
1
6

Next: