[algogeeks] Re: random number...
one of the solutions given at http://stackoverflow.com/questions/137783/given-a-function-which-produces-a-random-integer-in-the-range-1-to-5-write-a-fun is: int i; do { i = 5 * (rand5() - 1) + rand5(); // i is now uniformly random between 1 and 25 } while(i 21); // i is now uniformly random between 1 and 21 return i % 7 + 1; // result is now uniformly random between 1 and 7 Why do we need to go for all this trouble?? Why not: int i; do { i = rand5() + rand5(); // i is now uniformly random between 1 and 10 } while(i 7); // i is now uniformly random between 1 and 7 return i; Whats wrong with it?? On Sep 9, 12:34 am, Ramaswamy R ramaswam...@gmail.com wrote: Generate the random number 7 times. Sum them all and divide by 5. Theoritically it should be evenly distributed over 1-7. But owing to random number generators characteristics the sum of rand(5) called 7 times may not be perfectly evenly distributed over 1-7. A nice discussion on some neat options is available here -http://stackoverflow.com/questions/137783/given-a-function-which-prod... Rejection technique is pretty standard for this and yet simple. On Mon, Sep 7, 2009 at 8:56 AM, ankur aggarwal ankur.mast@gmail.comwrote: Given a random number generator that generates numbers in the range 1 to 5, how can u create a random number generator to generate numbers in the range 1 to 7. (remember that the generated random numbers should follow a uniform distribution in the corresponding range) -- Yesterday is History. Tomorrow is a Mystery. Today is a Gift! That is why it is called the Present :). http://sites.google.com/site/ramaswamyr --~--~-~--~~~---~--~~ 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 algogeeks+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/algogeeks -~--~~~~--~~--~--~---
[algogeeks] Re: probabiltiy + DP
i think there might be some modification On Thu, Sep 17, 2009 at 4:17 PM, Minjie Zha diego...@gmail.com wrote: Let PH(j,w) be the probability of getting w heads from 1...j coins, 0=j=k, 0=w=k. So we have: PH(0,0) = 1 PH( j, w ) = 0 if w 0 PH(0,w) = 0 for w0 PH(j,0) = (1-P(1))(1-P(2))...(1-P(j)) PH(j,w) = PH(j-1,w) + PH(j-1,w-1)PH(j) and equation should be PH(j, w) = PH(j-1,w) (1-P(j)) + PH( j-1, w-1) PH(j) pls correct if i am wrong... -- ashish Any comments? On Sep 9, 5:50 pm, Nagendra Kumar nagendra@gmail.com wrote: @all: There are k baised coins with probabilty of coming head is P(i) i = 1 to k. If all these coins are tossed together. find the probabilty of getting i heads ( i = k). think in Dynamic Programming. -Nagendra --~--~-~--~~~---~--~~ 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 algogeeks+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/algogeeks -~--~~~~--~~--~--~---
[algogeeks] Re: function problem
actually it depends upon whether condition (AB) and condition (CD) can occur simultaneously or these conditions are mutually exclusive. If these two conditions can't occur simultaneously then foo2() will be called 5000 * 75/100 = 3750 but if they can occur simultaneously then call to foo2() will be less than 3750 depending upon the overlapping of the both conditions. -- Ashish Gupta On Thu, Sep 17, 2009 at 3:32 PM, ankur aggarwal ankur.mast@gmail.comwrote: 5. void foo1() { if(AB) Then {_/* */} else if(CD) then foo2() } How many time foo2() would get called given AB 25% of the times and CD 75% of the times and foo1() is called 5000 times --~--~-~--~~~---~--~~ 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 algogeeks+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/algogeeks -~--~~~~--~~--~--~---
[algogeeks] Re: function problem
yeah!!! arun is right we can see same by probability!! AB have 1/4 probability so AB will be in 3/4 probability.. CD is also in 3/4.. to call foo2 probabiliy will be by production rule3/4*3/4=9/8 ans=9/8*5000 On Thu, Sep 17, 2009 at 12:50 PM, Arun arunm...@gmail.com wrote: else condition wud be called 75% times. of that 75% times cD wud be true so foo2 wud be called (0.75)*(0.75)*5000 times. On Thu, Sep 17, 2009 at 3:02 AM, ankur aggarwal ankur.mast@gmail.comwrote: 5. void foo1() { if(AB) Then {_/* */} else if(CD) then foo2() } How many time foo2() would get called given AB 25% of the times and CD 75% of the times and foo1() is called 5000 times --~--~-~--~~~---~--~~ 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 algogeeks+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/algogeeks -~--~~~~--~~--~--~---
[algogeeks] Re: function problem
Minor correction: 3/4 * 3/4 = 9/16, not 9/8. 110% probabilities occur only in politics. Dave On Sep 17, 3:08 pm, v...@ibh@V singhal vbhsing...@gmail.com wrote: yeah!!! arun is right we can see same by probability!! AB have 1/4 probability so AB will be in 3/4 probability.. CD is also in 3/4.. to call foo2 probabiliy will be by production rule3/4*3/4=9/8 ans=9/8*5000 On Thu, Sep 17, 2009 at 12:50 PM, Arun arunm...@gmail.com wrote: else condition wud be called 75% times. of that 75% times cD wud be true so foo2 wud be called (0.75)*(0.75)*5000 times. On Thu, Sep 17, 2009 at 3:02 AM, ankur aggarwal ankur.mast@gmail.comwrote: 5. void foo1() { if(AB) Then {_/* */} else if(CD) then foo2() } How many time foo2() would get called given AB 25% of the times and CD 75% of the times and foo1() is called 5000 times- 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 algogeeks@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 -~--~~~~--~~--~--~---
[algogeeks] Re: random number...
What is wrong with using i = rand5() + rand5() instead of i = 5 * rand5 () + rand5() is that the former is not uniformly distributed between 1 and 10, as claimed. First, 1 never occurs. 2 occurs 1 out of 25 times, 3 occurs 2 out of 25, etc. (Think of rolling two ordinary dice.) Dave On Sep 18, 6:19 am, eSKay catchyouraak...@gmail.com wrote: one of the solutions given athttp://stackoverflow.com/questions/137783/given-a-function-which-prod... is: int i; do { i = 5 * (rand5() - 1) + rand5(); // i is now uniformly random between 1 and 25} while(i 21); // i is now uniformly random between 1 and 21 return i % 7 + 1; // result is now uniformly random between 1 and 7 Why do we need to go for all this trouble?? Why not: int i; do { i = rand5() + rand5(); // i is now uniformly random between 1 and 10} while(i 7); // i is now uniformly random between 1 and 7 return i; Whats wrong with it?? On Sep 9, 12:34 am, Ramaswamy R ramaswam...@gmail.com wrote: Generate the random number 7 times. Sum them all and divide by 5. Theoritically it should be evenly distributed over 1-7. But owing to random number generators characteristics the sum of rand(5) called 7 times may not be perfectly evenly distributed over 1-7. A nice discussion on some neat options is available here -http://stackoverflow.com/questions/137783/given-a-function-which-prod... Rejection technique is pretty standard for this and yet simple. On Mon, Sep 7, 2009 at 8:56 AM, ankur aggarwal ankur.mast@gmail.comwrote: Given a random number generator that generates numbers in the range 1 to 5, how can u create a random number generator to generate numbers in the range 1 to 7. (remember that the generated random numbers should follow a uniform distribution in the corresponding range) -- Yesterday is History. Tomorrow is a Mystery. Today is a Gift! That is why it is called the Present :). http://sites.google.com/site/ramaswamyr- 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 algogeeks@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 -~--~~~~--~~--~--~---
[algogeeks] Re: random number...
On Sep 7, 11:56 am, ankur aggarwal ankur.mast@gmail.com wrote: Given a random number generator that generates numbers in the range 1 to 5, how can u create a random number generator to generate numbers in the range 1 to 7. (remember that the generated random numbers should follow a uniform distribution in the corresponding range) If you generate 2 random numbers, there are 25 possible outcomes. This doesn't map nicely to the 7 desired outcomes. The standard way out of this mess is to ignore 4 outcomes leaving 21. Then map these to the range 1 to 7: int n; do n = (random1to5() - 1) * 5 + (random1to5() - 1); while n = 7 * 3; return 1 + n % 7 You can avoid throwing away random numbers with higher probabilty by generating more to begin with. For example, generating 3 provides 5 * 5 * 5 = 125 possible outcomes. Discard 6 cases to leave 119 = 17 * 7. So we have int n; do n = (random1to5() - 1) * 5 * 5 + (random1to5() - 1) * 5 + (random1to5() - 1); while n = 7 * 17; return 1 + n % 7 The probability of discarding a number is 4/25 in the first case, but only 6 / 125 in the second. --~--~-~--~~~---~--~~ 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 algogeeks+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/algogeeks -~--~~~~--~~--~--~---