I’m trying to produce a 0 or 1 with numpy’s random.rand.
np.random.rand()
produces a random float between 0 and 1 but not just a 0 or a 1.
Thank you.
Advertisement
Answer
You can use np.random.choice with a list of [0,1]
, or use np.random.radint with a range of 0,2
In [1]: import numpy as np In [2]: np.random.choice([0,1]) Out[2]: 0 In [5]: np.random.choice([0,1]) Out[5]: 1 In [8]: np.random.randint(2) Out[8]: 0 In [9]: np.random.randint(2) Out[9]: 1
You can also use the random module for the equivalent of these functions