Skip to content
derpx06Notes on systems, models & learning
5. Data Work · lesson 13 of 17 · 1 min · January 1, 2024

Randomness

Generating random numbers and sampling.

As we learn data science, we will frequently need to generate random numbers, which we can do with the random module:

random.py
import random

four_uniform_randoms = [random.random() for _ in range(4)]

# [0.8444218515250481,
#  0.7579544029403025,
#  0.420571580830845,
#  0.25891675029296335]

# random.random() produces numbers
# uniformly between 0 and 1
# it's the random function we'll use most often

The random module actually produces pseudorandom (that is, deterministic) numbers based on an internal state that you can set with random.seed if you want to get reproducible results:

seed.py
random.seed(10)         # set the seed to 10
print random.random()   # 0.57140259469
random.seed(10)         # reset the seed to 10
print random.random()   # 0.57140259469 again

We’ll sometimes use random.randrange, which takes either 1 or 2 arguments and returns an element chosen randomly from the corresponding range():

randrange.py
random.randrange(10)    # choose randomly from range(10) = [0, 1, ..., 9]
random.randrange(3, 6)  # choose randomly from range(3, 6) = [3, 4, 5]

There are a few more methods that we’ll sometimes find convenient. random.shuffle randomly reorders the elements of a list:

shuffle.py
up_to_ten = range(10)
random.shuffle(up_to_ten)
print up_to_ten
# [2, 5, 1, 9, 7, 3, 8, 6, 4, 0]
# (your results will probably be different)

If you need to randomly pick one element from a list you can use random.choice:

choice.py
my_best_friend = random.choice(["Alice", "Bob", "Charlie"])
# "Bob" for me

And if you need to randomly choose a sample of elements without replacement (i.e., with no duplicates), you can use random.sample:

sample.py
lottery_numbers = range(60)
winning_numbers = random.sample(lottery_numbers, 6)
# [16, 36, 10, 6, 25, 9]

To choose a sample of elements with replacement (i.e., allowing duplicates), you can just make multiple calls to random.choice:

replacement.py
four_with_replacement = [random.choice(range(10))
                       for _ in range(4)]
# [9, 4, 4, 2]