Shuffling can be an interesting function in a program, even more if we want to ensure more randomness in a set of data that will be chosen.
We'll import the random
library and create a list of integers to use in our example.
import random
list = [10,20,30,40,50]
And finally, to shuffle the list, we'll use the shuffle
method. If we print the list, we can see the shuffled elements.
random.shuffle(list)
print(list)
Result example:
[50, 40, 20, 10, 30]
And to make a random choice, we use the choice
method.
x = random.choice(list)
print(x)
Result example:
30
Top comments (0)