Mastering Random Sampling in Python | by Sadrach Pierre, Ph.D. | Towards Data Science

Python provides many useful tools for random sampling as well as functions for generating random numbers. Random sampling has applications in statistics where often times a random subset of a population is observed and used to make inferences about the overall population. Further, random number generation has many application in the sciences. For example, in chemistry and physics Monte Carlo simulations require random number generation. In this post, we will discuss how to randomly sample items from lists as well as how to generate pseudorandom numbers in python.
Let’s get started!
The random module in python has many functions that are useful for generating random numbers and random sampling.
Consider a list of BMI values for people living in a rural area:
bmi_list = [29, 18, 20, 22, 19, 25, 30, 28,22, 21, 18, 19, 20, 20, 22, 23]
Let’s use the ‘random.choice()’ method to randomly select individual BMI values from this list:
import random print("First random choice:", random.choice(bmi_list))print("Second random choice:", random.choice(bmi_list))print("Third random choice:", random.choice(bmi_list))
If we run this code once more, we should get another set of randomly selected BMIs:
The ‘random.sample()’ method is useful for randomly sampling N items from a list. For example, if we’d like to sample N=5 items from our BMI list we do the following:
print("Random sample, N = 5 :", random.sample(bmi_list, 5))
Let’s try sampling 10 items:
print("Random sample, N = 10:", random.sample(bmi_list, 10))
In addition to random selection and sampling, the random module has a function for shuffling items in a list. Let’s print our BMI list and then print the result of shuffling our BMI list:
print("BMI list: ", bmi_list)random.shuffle(bmi_list)print("Shuffled BMI list: ", bmi_list)
The random module has a function for generating a random integer provided a range of values. Let’s generate a random integer in the range from 1 to 5:
print("Random Integer: ", random.randint(1,5))
Using this function, we can easily generate a list of random integers in a for-loop:
random_ints_list = []for i in range(1,50):    n = random.randint(1,5)    random_ints_list.append(n)print("My random integer list: ", random_ints_list)
The random module also has a function for generating a random floating point value between 0 and 1:
print("Random Float: ", random.random())
We can also generate a list of random floats between 0 and 1:
random_float_list = []for i in range(1,5):    n = random.random()    random_float_list.append(n)print("My random float list: ", random_float_list)
Further, we can scale the random float numbers. If we want random numbers between 0 and 500 we just multiply our random number by 500:
random_float_list = []for i in range(1,5):    n = random.random()*500    random_float_list.append(n)print("My random float list: ", random_float_list)
And if we want to add a lower bound as well we can add a conditional statement before appending. For example to generate random numbers between 100 and 500 we do the following:
random_float_list = []for i in range(1,10):    n = random.random()*500    if n>=100.0:        random_float_list.append(n)print("My random float list: ", random_float_list)
The random module has a function for computing uniformly distributed numbers. For example, to generate 50 uniformly distributed numbers between -10 and 1 we do the following:
import numpy as npuniform_list = np.random.uniform(-10,1,50)print("Uniformly Distributed Numbers: ", uniform_list)
Finally, the random module has a function for computing normally distributed numbers. For example, to generate 50 normally distributed numbers between -50 and 0 we do the following:
normal_list = np.random.uniform(-50,0,50)print("Normally Distributed Numbers: ", normal_list)
I’ll stop here but I encourage you to play around with the code yourself.
To summarize, we discussed how to randomly select and sample items from lists in python. We showed how to use the ‘random.choice()’ method to select a single item randomly from a list. We also used the ‘random.sample()’ method, which allows you to randomly select N items from a list. We also discussed how to shuffle items in a list using the ‘random.shuffle()’ method. Additionally, we showed how to generate random numbers using the random module. We generated random integers using ‘random.randint()’and random floating point values using ‘random.random()’. Finally, we went over how to generate uniformly and normally distributed numbers with ‘random.uniform()’ and ‘random.gauss()’ respectively. I hope you found this post useful/interesting. The code in this post is available on GitHub. Thank you for reading!