> Here's my lame attempt. Output is uniformly distributed if input is so.
>
> 1 -> 20.065%
> 2 -> 19.986%
> 3 -> 19.989%
> 4 -> 19.964%
> 5 -> 19.996%
>
>
> 1 -> 14.352%
> 2 -> 14.302%
> 3 -> 14.281%
> 4 -> 14.285%
> 5 -> 14.295%
> 6 -> 14.252%
> 7 -> 14.233%
>
> /* */
> #include<stdio.h>
> #include<stdlib.h>
> #include<string.h>
>
> int rand5()
> {
>         return (rand() % 5)+1;
> }
>
> int rand2()
> {
>         int bit;
>         while((bit=rand5())==5);
>         return bit%2;
> }
>
>
> int rand7()
> {
>         int sum;
>         while( (sum = rand2() + rand2()*2 + rand2()*4) == 0);
>         return sum;
> }
>
>
> int main()
> {
>         int freq1[6];
>         int freq2[8];
>
>         memset(freq1, 0, sizeof(freq1));
>         memset(freq2, 0, sizeof(freq2));
>
>         const int ITER = 1000000;
>         for(int i=0; i<ITER; i++) {
>                 freq1[rand5()]++;
>                 freq2[rand7()]++;
>         }
>
>         for(int i=1; i<=5; i++)
>                 printf("%d -> %.3lf%%\n", i, double(freq1[i])/ITER*100);
>         printf("\n\n");
>         for(int i=1; i<=7; i++)
>                 printf("%d -> %.3lf%%\n", i, double(freq2[i])/ITER*100);
>         return 0;
> }
>
> But of course it is not guaranteed to terminate but it works usually :-)
>
> atamurat
> >
> >
> > On Jan 31, 7:29 am, "Ming \(Amos\) Zhang" <[EMAIL PROTECTED]> wrote:
> > > It's not uniformly distributed, suppose the given random generator is
> > > uniformly distributed
> > >
> > > -----Original Message-----
> > > From: algogeeks@googlegroups.com [mailto:[EMAIL PROTECTED]
> On
> > >
> > > Behalf Of Sandesh
> > > Sent: Tuesday, January 30, 2007 11:09 PM
> > > To: Algorithm Geeks
> > > Subject: [algogeeks] Re: (need help) How to solve this random number
> > > generatioin problem?
> > >
> > > suppose "given" function returns the random numbers between 1 -5  then
> > > you can have
> > >
> > >        (given + given) % 7 +1
> > >
> > > which will generate between 1 and 7 .
> > >
> > >        -Sandesh Hegde
> > >
> > > On Jan 31, 6:57 am, "Jialin" <[EMAIL PROTECTED]> wrote:
> > > > Question:
> > >
> > > > Given a program which can generate one of {1, 2, 3, 4, 5} randomly.
> > > > How can we get another generator which can generate one of
> > > > {1,2,3,4,5,6,7} randomly?
> > >
> > > > Thank you!
> >
> >
> > >
> >
>
> I dont think its uniformly distributed, for the simple reason that , in
> the binary representation of the numbers from 1-7 there are more zeros than
> 1's. Am I missing anything?
>

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups-beta.google.com/group/algogeeks
-~----------~----~----~----~------~----~------~--~---

Reply via email to