@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 produced by f match the high order bit sequence of p. This
> most likely will use fewer evaluations of f() than Don's approach.
>
> int g(double p)
> {
>     int i;
>     do
>     {
>         i = p + p;
>         p += p - i;
>     } while( i == f() );
>     return 1 - i;
>
> }
>
> Dave
>
> On Sep 12, 10:19 am, 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 > (int)(p*1073741824.0);
>
> > }
>
> > On Sep 12, 9:55 am, JITESH KUMAR <jkhas...@gmail.com> wrote:
>
> > > Hi
> > > You are given a function f() that returns either 0 or 1 with equal
> > > probability.
> > > Write a function g() using f() that return 0 with probability p (where 
> > > 0<p<1
> > > )
>
> > > --
> > > *Regards
> > > Jitesh Kumar*- Hide quoted text -
>
> > - Show quoted text -

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to