I have been reading up about the random.sample()
function in the random
module and have not seen anything that solves my problem.
I know that using random.sample(range(1,100),5)
would give me 5 unique samples from the 'population'...
I would like to get a random number in range(0,999)
. I could use random.sample(range(0,999),1)
but why then am I thinking about using random.sample()
?
I need the random number in that range to not match any number in a separate array (Say, [443,122,738]
)
Is there a relatively easy way I could go about doing this?
Also, I am pretty new to python and am definitely a beginner -- If you would like me to update the question with any information I may have missed then I will.
EDIT:
Accidentally said random.range()
once. Whoops.
There are two main ways:
Example usage:
Output:
The first is better for a smaller range or a larger list
exclude
(so thechoices
list won't be too big), the second is better for the opposite (so it doesn't loop too many times looking for an appropriate option).One way you can accomplish that is by simply checking the number and then appending it to a list where you can then use the numbers.