Let’s say we are given a list of numbers. We want to generate a random sample from the list. In Python, we can do so easily using the sample() function from the random module.
from random import seed from random import sample seed(1) data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] subset = sample(data, 3) print(subset)
We first use the seed() function to initialize the pseudo-random number generator. After that, we use the sample() function to create a subset from the list of numbers. The second argument of the sample() function is the size of the sample.
Please note that if we use the same seed value multiple times, then the sample() function will generate the same sample each time from the given list of numbers. For example, here, we are using the seed value 1. So, if we run the program multiple times, the output will be the same.
The above program will generate an output like the following:
[3, 2, 5]






0 Comments