int bit_generator(); // function which returns 1 and 0 with equal probabilities
int generator()
{
int generated_num[10],i,n;
for(i=0;i<10;i++)
{
generated_num[i]=bit_generator();
}
n=generated_num[0];
i=1;
while(i<10)
{
n*=10;
n+=generated_num[i];
i++;
}
if(n==0 || n>1000)
generator();
else
return n;
}
No. of possible outcomes = 1000 i.e. 1 to 1000
Since each outcome is equally likely, so probability of each generated
no. is 1/1000.
There are some issues with this algo, since we are calling generator()
again and again untill the no. is from 1-1000, but we can discuss on
these issues...
--
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.