+ 1

how to use random

12th Apr 2017, 5:35 PM
adi
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
12th Apr 2017, 7:44 PM
Gami
Gami - avatar
+ 6
You would mostly use pseudo-random numbers (integers) so just import random. % or range are useful
12th Apr 2017, 5:43 PM
Dinmukhamed Mailibay
Dinmukhamed Mailibay - avatar
+ 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.
12th Apr 2017, 6:35 PM
Klaus-Dieter Warzecha
Klaus-Dieter Warzecha - avatar