[Numpy-discussion] random number genration

Daniel Lepage dplepage@gmail....
Tue Mar 29 08:46:05 CDT 2011


On Tue, Mar 29, 2011 at 5:00 AM, Alex Ter-Sarkissov <ater1980@gmail.com> wrote:
> If I want to generate a string of random bits with equal probability I run
>
> random.randint(0,2,size).
>
> What if I want a specific proportion of bits? In other words, each bit is 1
> with probability p<1/2 and 0 with probability q=1-p?

x = (np.random.random(size) < p)

Setting p = .5 should produce the same results as
np.random.randint(0,2,size). Note that this gives you an array of
bools, not ints; use x.astype(int) if integers are important (or
x.astype(np.uint8) if memory is an issue).

HTH,
Dan Lepage


More information about the NumPy-Discussion mailing list