Random gen() picks a repeated number, the same number as 33, 33, in a list. I want to get a different list from numbers [0.99]. What method should be used to fix the bug?
CodePudding user response:
the random library provide you with a function for this in sample
>>> import random
>>> random.sample(range(100),5)
[86, 27, 78, 74, 57]
>>>
if you only want one number you can use either choice or randint
>>> random.choice(range(100))
23
>>> random.randint(0,99)
77
>>>
CodePudding user response:
Here is a numpy
solution using np.random.choice()
. Setting the flag replace=False
avoids duplicates.
import numpy as np
draws = 6
np.random.choice(range(0,100), draws, replace=False)
Instead of passing range(100)
, you could also pass 100, which will return numbers between 0-99.
Output:
array([76, 74, 2, 37, 96, 41])
CodePudding user response:
# import random (library to generate random numbers)
import random
# declare a variable l_number (lottery number) using the function random.choice()
l_number = random.choice(range(0, 100))
# print the lottery number
print(l_number)