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.07708512963860659
0.121536997477391
0.46485824437440637
0.2526223121677368
0.5602257437744373

Random values in [0,100)
55.40979691612028
34.018872524262825
48.03860613216406
29.048422625543946
0.5266478932496987

Random integers in [1,10]
10
7
2
7
9

Next: