[algogeeks] Re: Returning 0 with probability p and 1 with probability 1-p

2011-09-13 Thread Dave
@Jitesh: I did. But here is another try: We construct a fraction p' by using the random number generator to select the bits. So the number is 0.f()f()f()f()..., where each f() is a zero- or a one-bit. We compare p to p'. If, no matter what remaining bits we choose for p' we will have p p', then

[algogeeks] Re: Returning 0 with probability p and 1 with probability 1-p

2011-09-13 Thread Don
@Dave: Very nice. Don On Sep 12, 10:51 pm, Dave dave_and_da...@juno.com wrote: Here's another way, using a rejection technique on the bits of the mantissa of p. Each iteration of the do-while loop exposes another high-order bit of p, and the do-while loop iterates as long as the random bits

Re: [algogeeks] Re: Returning 0 with probability p and 1 with probability 1-p

2011-09-13 Thread JITESH KUMAR
@Dave: Thanks a lot.. :) On Tue, Sep 13, 2011 at 7:48 PM, Don dondod...@gmail.com wrote: @Dave: Very nice. Don On Sep 12, 10:51 pm, Dave dave_and_da...@juno.com wrote: Here's another way, using a rejection technique on the bits of the mantissa of p. Each iteration of the do-while loop

[algogeeks] Re: Returning 0 with probability p and 1 with probability 1-p

2011-09-13 Thread Don
Here is an extension: Given a pseudo-random generator f() which return unsigned integers uniformly distributed over the range 0..2^32-1, create a generator which produces integer values in the range 0..N-1 with each occurring with a probability {p0, p1, p2, ..., pn-1} such that all the

[algogeeks] Re: Returning 0 with probability p and 1 with probability 1-p

2011-09-12 Thread Don
For particular values of p we might be able to do better, but for unknown values of p, I can't think of anything better than this: int g(double p) { int n = 0; for(int i = 0; i 30; ++i) n += n+f(); return n (int)(p*1073741824.0); } On Sep 12, 9:55 am, JITESH KUMAR jkhas...@gmail.com

Re: [algogeeks] Re: Returning 0 with probability p and 1 with probability 1-p

2011-09-12 Thread Anup Ghatage
Don, how did you come up with 1073741824.0 ? On Mon, Sep 12, 2011 at 8:49 PM, Don dondod...@gmail.com wrote: For particular values of p we might be able to do better, but for unknown values of p, I can't think of anything better than this: int g(double p) { int n = 0; for(int i = 0; i

Re: [algogeeks] Re: Returning 0 with probability p and 1 with probability 1-p

2011-09-12 Thread siddharth srivastava
On 12 September 2011 20:49, Don dondod...@gmail.com wrote: For particular values of p we might be able to do better, but for unknown values of p, I can't think of anything better than this: int g(double p) { int n = 0; for(int i = 0; i 30; ++i) n += n+f(); return n

[algogeeks] Re: Returning 0 with probability p and 1 with probability 1-p

2011-09-12 Thread Don
It uses 30 random bits from f() to build a 30-digit number which will be in the range 0..2^30. 2^30 is 1,073,741,824. p*2^32 is the number of values in that range which should produce the result 0. Don On Sep 12, 10:48 am, siddharth srivastava akssps...@gmail.com wrote: On 12 September 2011

[algogeeks] Re: Returning 0 with probability p and 1 with probability 1-p

2011-09-12 Thread Dave
Here's another way, using a rejection technique on the bits of the mantissa of p. Each iteration of the do-while loop exposes another high-order bit of p, and the do-while loop iterates as long as the random bits produced by f match the high order bit sequence of p. This most likely will use fewer

Re: [algogeeks] Re: Returning 0 with probability p and 1 with probability 1-p

2011-09-12 Thread JITESH KUMAR
Hi Dave, Can you please explain your approach? On Tue, Sep 13, 2011 at 9:21 AM, Dave dave_and_da...@juno.com wrote: Here's another way, using a rejection technique on the bits of the mantissa of p. Each iteration of the do-while loop exposes another high-order bit of p, and the do-while loop