Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Learn/Python Overview/Part 5 · 5. Data Work
Lesson 13 of 17

Randomness

Generating random numbers and sampling.

Derpx061 Jan · 1 min read
Python Overview
RandomnessRegular ExpressionsObject-Oriented ProgrammingIterators & ZipArgs & Kwargs
0% read · 1 min leftBack to top ↑
Course outline & contents0%
Python Overview
RandomnessRegular ExpressionsObject-Oriented ProgrammingIterators & ZipArgs & Kwargs

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]
Track your progressSign in and your progress follows you across devices.
Sign in to track progress Next lesson
← Previous lessonFunctional ToolsNext lesson →Regular Expressions