Let’s say we have a list of numbers. And we want to select a number from the list of numbers randomly. In Python, we can do so easily using the choice function from the random module.
from random import seed from random import choice seed(1) data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] number = choice(data) print(number)
Here, we first seed the random number generator so that the pseudo-random number generator uses the seed value to initialize itself. After that, we use the choice() function from the random module to select a number from the list of given numbers randomly.
Please note that if we use the same seed value multiple times, then the choice() function will select the same number from the given list each time. In other words, if we use the seed value 1 twice and run the program twice on the same list of numbers, then the output of the program will be the same each time.
The output of the above program will be like the following:
3








































0 Comments