@snehal...a simple way to do it is..

create an array of lets say 1000...

fill in 1000* p elements with 0 and the rem with 1

now use rand() and generate the index..and return arr[index]

On Fri, Oct 8, 2010 at 8:49 PM, Dave <dave_and_da...@juno.com> wrote:

> @Snehal: If p has a finite binary representation, of say n bits, then
> generate a sequence of up to n unbiased random numbers, continuing the
> sequence as long as the ith random number agrees with the ith bit to
> the right of the binary point. When the ith number disagrees with the
> ith bit, return that ith number. If the computation continues until
> even the nth number matches the nth bit, then start over.
>
> Example: Suppose that the binary representation of p is 0.101101.
> If the first unbiased random number is 1, generate the second number.
> If the second number is 0, continue to the third number.
> If the third number is 0, return 0.
>
> If p does not have a finite binary representation, e.g., if p = 1/3 or
> sqrt(1/2) and you are not using a finite precision floating point
> representation, then the algorithm will terminate with probability 1.
>
> Dave
>
> On Oct 8, 2:44 am, snehal jain <learner....@gmail.com> wrote:
> > Given a unbiased function that generates 0 and 1 with equal
> > probability write a function biasedrandom that genreates 0 with
> > probability p and 1 with probability 1-p.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algoge...@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com<algogeeks%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>


-- 
S.Nishaanth,
Computer Science and engineering,
IIT Madras.

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algoge...@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