Lets say I want to generate one of the numbers 1, 4, or 7.
How would I do this? I originally thought I could write
import random
rand.randint(1,4,7)
but that doesn't seem to work. Thanks.
Use random choice
print random.choice([1,4,7])
import random
import string
def random_number(length):
return [random.choice(string.digits) for x in range(length)]
>>> random_number(5)
['0', '1', '1', '9', '0']
>>> random_number(2)
['5', '9']
>>> random_number(1)
['0']
>>> random_number(11)
['2', '3', '4', '7', '1', '8', '1', '9', '3', '6', '9']
>>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With