+ 1
how to use random
3 Answers
+ 10
You can generate random numbers, or random variables from an array...
import random
lst = [9,3,"a"]
print(random.randint(0,100) # Generates random integer between 0 and 100
print(random.choice(lst)) # Prints a random variable from lst
+ 6
You would mostly use pseudo-random numbers (integers) so just import random. % or range are useful
+ 1
Typically you will
import random
and use some of the module's methods. There's is much more than just a random integer.
random.sample is fun to pick a selection of unique elements from a list (or set). Just the rigth thing if you need to draw numbers from a lottery.
random.choice is cool to pick an element from a non-empty sequence.
Just have a look at https://code.sololearn.com/cvIah7M2grUG to play with it.