29 Mar
2011
29 Mar
'11
1:46 p.m.
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