Is there a simple way in Python to generate a random number in a range excluding some subset of numbers in that range?
For example, I know that you can generate a random number between 0 and 9 with:
from random import randint
randint(0,9)
What if I have a list, e.g. exclude=[2,5,7]
, that I don't want to be returned?
Select the cells in which you want to get the random numbers. In the active cell, enter =RAND() Hold the Control key and Press Enter. Select all the cell (where you have the result of the RAND function) and convert it to values.
You can restrict the random numbers between a certain range by providing the minimum and maximum values as arguments. In addition to Random. ints() , Java 8 also introduced Random. doubles() and Random.
Try this:
from random import choice
print choice([i for i in range(0,9) if i not in [2,5,7]])
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