There is an infinitesimally small probability that it will continue to
run forever. But I tested it for 10,000 runs and it ran in a flash on
my archaic machine (5yrs old is archaic probably :) )
int generateRand()
{
int num = 1;
for(int i=0;i<10;i++)
{
int x = (int)pow(2.0,i);
num += x * (rand() % 2 );
}
if(num <= 1000)
return num;
else
return generateRand();
}
On Jul 15, 2:31 pm, SkRiPt KiDdIe <[email protected]> wrote:
> You are provided with a bit generator which generates 1 and 0 with equal
> probabilities i.e. (1/2). You are required to design a function which
> generates numbers form 1-1000 with equal probabilities i.e. (1/1000).
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.