How do you produce a random 0 or 1 with random.rand

Question:

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.

Asked By: martinb

||

Answers:

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

Answered By: Devesh Kumar Singh

You can use numpy.random.random_integers

random_int= np.random.random_integers(0,1)
print (random_int)

You can use np.random.randint(low, high=None, size=None).

>>> np.random.randint(0,2,10)
array([0, 1, 1, 0, 1, 1, 0, 0, 1, 0])
>>> np.random.randint(2)
0
>>> np.random.randint(2)
1

Fore more details, you can refer to https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.randint.html

Answered By: ToughMind

You should consider using np.random.randint() This function takes a range as an input.
For example,

>>> np.random.randint(2)

This will give you an output of either 0 or 1

Answered By: Aditya Dwivedi

To add to the other answers, it is possible to simply do

p_True = 0.5 # 50% probability that you get 1
your_bool = p_True >= np.random.rand() # >= because rand returns a float between 0 and 1, excluding 1.

You can have a biased sample by changing p_true.

Answered By: mknull

Is there a reason to specifically use np.random.rand? This function outputs a float as noted in the question and previous answers, and you would need thresholding to obtain an int.

scipy.stats.bernoulli(p) directly outputs a 1 with probability p and 0 with probability 1-p.

Answered By: Geoffrey Negiar

You can use np.random.choice with a list of [0,1] and a size to get a random choice matrix like this:

In [1]: import numpy as np

In [2]: np.random.choice([0,1], size=(3,4))
Out[2]: array([[1, 0, 0, 0],
               [0, 1, 1, 0],
               [1, 1, 1, 1]])
Answered By: Alexander Martins
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.