@Sony. I took (2/3)*rand5() as if (2/3) was a double. Otherwise, (2/3)*rand5() = 0*rand5() = 0, which is not what you calculated when you wrote 2*(i/3) instead of (2/3)*i.
And yes, the fact that the distribution is not uniform does matter, since we want the results to be uniform. Using your interpretation of (2/3)*rand5(), to get i = 1, the first rand5() must be 1 and the second rand5() must be 1 or 2. Thus, the probability of i = 1 is 1/5 * 2/5 = 2/25. To get i = 2, the first rand5() must be 2 and the second rand5() must be 1 or 2. Thus, i = 2 also with probability 1/5 * 2/5 = 2/25. For i = 3, either the first rand5() must be 1 and the second rand5() must be 3, 4, or 5, or the first rand5 must be 3 and the second rand5() must be 1 or 2. Thus, i = 3 with probability 1/5 * 3/5 + 1/5 * 2/5 = 1/5. Continuing in this way, we see that the probabilities of the various results are 2/25, 2/25, 1/5, 1/5, 1/5, 3/25, 3/25. These are not uniform! Dave On Jul 31, 8:12 am, Sony Jose <sonyj...@gmail.com> wrote: > Hi Dave, > > i just checked; > > int i; > int j; > > for(i=1;i<6;i++) { > j = 2*(i/3); > printf("\n%d\n",j); > } > > gives me output 0,0,2,2,2. and there was no 3; > > and since we are anyway generating "random" numbers would this difference in > distribution really matter? > > > > > > On Sat, Jul 31, 2010 at 6:30 PM, Dave <dave_and_da...@juno.com> wrote: > > @Sony. No. Consider the following table: > > > rand5() (2/3)*rand5() > > _________________________ > > 1 0 > > 2 1 > > 3 2 > > 4 2 > > 5 3 > > > Thus, (2/3)*rand5() is not uniformly distributed, nor is it in the > > range 0 to 2. > > > Dave > > > On Jul 31, 7:49 am, Sony Jose <sonyj...@gmail.com> wrote: > > > what about > > > > int i = rand5() + (2/3)*rand5(); > > > > won't this work? > > > > On Sat, Jul 31, 2010 at 5:46 PM, Dave <dave_and_da...@juno.com> wrote: > > > > Use the rejection method... > > > > > int rand7() > > > > { > > > > int i; > > > > do > > > > i = 5 * rand5() + rand5() - 3; > > > > while( i > 23 ); > > > > return i / 3; > > > > } > > > > > The loop assigns i a value between 5*1+1-3 = 3 and 5*5+5-3 = 27 with > > > > uniform probability, and then rejects any value of i > 23. Thus, after > > > > the loop, i has a uniform distribution on the interval 3 to 23. > > > > Dividing by 3 gives the desired result. > > > > > Under the assumptions of the problem, a value of i is rejected with > > > > probability 4/25, so the loop executes an average of 1/(1 - 4/25) = > > > > 25/21 times. Therefore, on average, it takes 50/21 rand5's to make a > > > > rand7. > > > > > Dave > > > > > On Jul 30, 8:33 am, jalaj jaiswal <jalaj.jaiswa...@gmail.com> wrote: > > > > > given a rand5 function which generates numbers from 1 to 5 with equal > > > > > probability.. give an algorithm which uses rand5 function and > > generates > > > > > numbers from 1 to 7 with equal probability > > > > > -- > > > > > With Regards, > > > > > Jalaj Jaiswal > > > > > +919026283397 > > > > > B.TECH IT > > > > > IIIT ALLAHABAD > > > > > -- > > > > 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> > > <algogeeks%2bunsubscr...@googlegroups.com> > > > > . > > > > For more options, visit this group at > > > >http://groups.google.com/group/algogeeks?hl=en. > > > > -- > > > Regards, > > > Sony- 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 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. > > -- > Regards, > Sony- 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 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.