Binary random array with a specific proportion of ones?

前端 未结 6 633
-上瘾入骨i
-上瘾入骨i 2020-11-27 03:05

What is the efficient(probably vectorized with Matlab terminology) way to generate random number of zeros and ones with a specific proportion? Specially with Numpy?

6条回答
  •  攒了一身酷
    2020-11-27 03:42

    Yet another approach, using np.random.choice:

    >>> np.random.choice([0, 1], size=(10,), p=[1./3, 2./3])
    array([0, 1, 1, 1, 1, 0, 0, 0, 0, 0])
    

提交回复
热议问题