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.16710191253182538
0.6997760854793376
0.7786289788138732
0.45781536766789754
0.9459602838111884

Random values in [0,100)
62.73587283534938
13.244884683462931
23.243001564323496
24.689734601429258
31.76541380111274

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

Next: