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.1412339580488201
0.028803415753871908
0.34848952778410314
0.160824919666252
0.2597760419111206

Random values in [0,100)
16.81253861678993
18.920694687471695
21.442368636942145
63.9255714993834
6.00400837787638

Random integers in [1,10]
9
7
10
6
3

Next: