@Debajyoti. There are 25 possible values of 5*rand5() + rand5(). The largest multiple of 7 not exceeding 25 is 3*7. Thus, I divide by 3. The largest number n such that n/3 = 7 is 23. This is where the 3 and 23 come from. You might google "rejection method" to find fuller descriptions of the theory behind these methods.
Dave On Jul 31, 8:04 am, Debajyoti Sarma <[email protected]> wrote: > why u have chosen that 23 ? > why dividing by 3 ? > don't understand the logic. > Please explain so that it become understandable. > > On 7/31/10, Dave <[email protected]> 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 <[email protected]> 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 [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.- 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 [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.
