tag:blogger.com,1999:blog-17722541.post7600843643259865893..comments2023-10-30T12:41:52.941+01:00Comments on don't code today what you can't debug tomorrow: Random number 1..5 to 1..7Ariya Hidayathttp://www.blogger.com/profile/03121582140059106015noreply@blogger.comBlogger47125tag:blogger.com,1999:blog-17722541.post-21781182661921276732011-07-14T18:01:36.167+02:002011-07-14T18:01:36.167+02:00@Chen: no, it's not.@Chen: no, it's not.Ariya Hidayathttps://www.blogger.com/profile/03121582140059106015noreply@blogger.comtag:blogger.com,1999:blog-17722541.post-48195368535548639692011-07-14T17:45:27.404+02:002011-07-14T17:45:27.404+02:00(rand5()+rand5()+rand5()+rand5()+rand5()+rand5()+r...(rand5()+rand5()+rand5()+rand5()+rand5()+rand5()+rand5())%7<br />Which should be uniformChennoreply@blogger.comtag:blogger.com,1999:blog-17722541.post-44241412338448902062011-07-05T07:53:18.599+02:002011-07-05T07:53:18.599+02:00@Anonymous: Adding two random numbers does not pro...@Anonymous: Adding two random numbers does not produce uniform distribution, check other comments.Ariya Hidayathttps://www.blogger.com/profile/03121582140059106015noreply@blogger.comtag:blogger.com,1999:blog-17722541.post-16194761742772432782011-07-03T23:22:50.466+02:002011-07-03T23:22:50.466+02:00Ariya thanks for this blog entry.
One question, w...Ariya thanks for this blog entry.<br /><br />One question, why do you choose to run rand twice? Can you not just run rand5 twice and throw out any invalid values?<br /><br />For example, assuming rand5 return 0 to 4, then<br /><br />int rand7() <br />{<br /> int result = rand5 + rand5;<br /> if (result <= 6)<br /> return result + 1;<br /> else<br /> return rand7();<br />}<br /><br />Wouldn't this return you back a uniformly distributed value between 1 and 7?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-17722541.post-56644099264668613402011-06-30T18:01:00.687+02:002011-06-30T18:01:00.687+02:00@Shriram: The first solution rand7() "has zer...@Shriram: The first solution rand7() "has zero probability for generating numbers 6 and 7" which is perfectly fine if uniform distribution is not mandated. not uniform = no obligation to generate 6 or 7 or any others (regardless the range).Ariya Hidayathttps://www.blogger.com/profile/03121582140059106015noreply@blogger.comtag:blogger.com,1999:blog-17722541.post-57400261714497208092011-06-29T05:17:49.176+02:002011-06-29T05:17:49.176+02:00The first solution
rand7()
{
rand5()
}
will not...The first solution<br /><br />rand7()<br />{<br /> rand5()<br />}<br /> will not gives values ranging from 6, and 7.<br />even though it didn't say it requires uniform distribution.<br />when it expects a number from the range 1-7,<br />it requires for the function to generate atleast once or 1/7 probability of generating numbers 6 and 7.<br />whereas rand7() has zero probability for generating numbers 6 and 7Shriramhttp://mavericktime.blogspot.comnoreply@blogger.comtag:blogger.com,1999:blog-17722541.post-71108131867400979912011-05-28T01:04:12.179+02:002011-05-28T01:04:12.179+02:00@Alexei Sergeev
That is wrong if you want uniform ...@Alexei Sergeev<br />That is wrong if you want uniform distribution.Mike Kobithttps://www.blogger.com/profile/07688794646185761005noreply@blogger.comtag:blogger.com,1999:blog-17722541.post-41110082872141701232010-10-02T19:11:57.507+02:002010-10-02T19:11:57.507+02:00@Anonymous: The problem is not unknown, see why I ...@Anonymous: The problem is not unknown, see why I wrote "static". Think of each step as a progressive improvement, following the train of thoughs, towards reaching the ultimate answer.Ariya Hidayathttps://www.blogger.com/profile/03121582140059106015noreply@blogger.comtag:blogger.com,1999:blog-17722541.post-74332985169097143012010-10-02T18:31:39.040+02:002010-10-02T18:31:39.040+02:00I think one of your solution has a problem.
int r...I think one of your solution has a problem.<br /><br />int rand7()<br />{<br /> static int c = 0;<br /> int x = rand5() + c;<br /> c = (c + 5*rand5()) % 35;<br /><br /> return (x % 7)+1;<br />}<br /><br />This solution can return uniform distribution. However variable x has memory property because of variable c. This means the previous result effect to the next result. If we know current return value we can guess a value can be returned with higher probability than others'.<br /><br />(And.. sorry for my poor Engligh...)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-17722541.post-23953243586415582852010-05-07T06:40:59.321+02:002010-05-07T06:40:59.321+02:00Hi there :)
I don't think a perfect solution ...Hi there :)<br /><br />I don't think a perfect solution is possible without throwing out some answers. Thus in terms of code an IF clause of some sort is going to be required.<br /><br />One neat and almost perfect solution I've come up with in addition to what others have listed is as follows:<br /><br />1. Run the rand5 function six times.<br />2. Assuming you get six random digits 0-4 from step one, you will now have a completely random six-digit number in base-5.<br />3. A six-digit number in base-5 has a maximum value of 15,624. That number is divisible by 7, so you can simply divide it into even shelves and have a perfectly random result.<br /><br />* The trouble with this method is, of course, that 000000 = 0 is a valid number, so there are in fact 15,625 numbers, not 15,624. Actually pretty obvious considering only a power of 5 would make sense. We can simply replace some of your above code using an IF statement and discarding 000000 ... that would mean that we'd get a valid answer 15,624 / 15,625 times on the first try.Georgehttps://www.blogger.com/profile/17513177305057943547noreply@blogger.comtag:blogger.com,1999:blog-17722541.post-62982750748919150822010-01-11T07:55:07.387+01:002010-01-11T07:55:07.387+01:00Hmm, many answers are not quite right.
If you sol...Hmm, many answers are not quite right.<br /><br />If you solve system:<br />1*x = 1+y,<br />5*x = 7 + y.<br /><br />x=1.5, y=0.5<br /><br />So, our function is<br /><br />int random7()<br />{<br />return(1.5*random5() - 0.5);<br />}mighty-whityhttps://www.blogger.com/profile/08022031113243464793noreply@blogger.comtag:blogger.com,1999:blog-17722541.post-9408622751445447212010-01-11T07:53:22.153+01:002010-01-11T07:53:22.153+01:00This comment has been removed by the author.mighty-whityhttps://www.blogger.com/profile/08022031113243464793noreply@blogger.comtag:blogger.com,1999:blog-17722541.post-13282234667696032752009-04-20T12:35:00.000+02:002009-04-20T12:35:00.000+02:00The simplest and more effective way to generate ra...The simplest and more effective way to generate random number with required distribution is tossing coin experiment. As you all know, in probability theory tossing coin experiment is used to generate independent events. The following algo perfectly maintains the probability distribution. rand5() is only distraction to the programmer, so to generate one need ability to toss coin, i.e. any random function. There is no limit range one can generate, it will be effective if the random number generate is between 0-2**N.<br /><br /> int val =0, c,j,shi=0;<br /> for(j=0;j<3;j++)<br /> {<br /> ran = rand5();<br /> shi = (ran>2)?1:0;<br /> shi = shi<< j;<br /> val = val | shi;<br /> }Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-17722541.post-70465353786708971192009-03-05T03:20:00.000+01:002009-03-05T03:20:00.000+01:00This comment has been removed by the author.Anonymoushttps://www.blogger.com/profile/16609622446616790516noreply@blogger.comtag:blogger.com,1999:blog-17722541.post-29686172484116768682008-12-10T12:41:00.000+01:002008-12-10T12:41:00.000+01:00What about:array = new array[7];Step1: for (i=0..a...What about:<BR/><BR/>array = new array[7];<BR/><BR/>Step1: for (i=0..array.size-1) array[i] = rand5();<BR/><BR/>Step2: find max = max{array[i]};<BR/><BR/>Step3: build array1 = {array[i] | array[i] = max}, so "array1" is a subset of "array"<BR/><BR/>Step4: IF array1.size == 1 THEN return its element ELSE array = array1 and repeat Step 1<BR/>...<BR/>Repeat until exactly 1 element is in "array1".<BR/><BR/>Distribution seems to be quite uniform.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-17722541.post-31436168830566645272008-09-28T06:24:00.000+02:002008-09-28T06:24:00.000+02:00This comment has been removed by the author.Antoniohttps://www.blogger.com/profile/10243695698370000310noreply@blogger.comtag:blogger.com,1999:blog-17722541.post-16618216201564489902008-05-13T03:30:00.000+02:002008-05-13T03:30:00.000+02:00I didn't consider permutations because they are co...I didn't consider permutations because they are combinations of the three outcomes. Combinations with repetition to be more precise.<BR/><BR/>Because I am adding the value of the three outcomes in each experiment, so 1+2+3 is the same as 3+2+1<BR/><BR/>I have detailed the full explanation at http://www.keyvez.netAnonymoushttps://www.blogger.com/profile/02708419146799403511noreply@blogger.comtag:blogger.com,1999:blog-17722541.post-43760667004235098572008-05-13T03:18:00.000+02:002008-05-13T03:18:00.000+02:00@keyvez: You didn't take into account of all 125 (...@keyvez: You didn't take into account of all 125 (5*5*5) permutations. Here's the distribution of your approach:<BR/>1: 18 times<BR/>2: 19 times<BR/>3: 19 times<BR/>4: 19 times<BR/>5: 18 times<BR/>6: 16 times<BR/>7: 16 timesUnknownhttps://www.blogger.com/profile/02164287150350329137noreply@blogger.comtag:blogger.com,1999:blog-17722541.post-78490483493266965982008-05-13T02:25:00.000+02:002008-05-13T02:25:00.000+02:00I am also suspicious of my method, could you pleas...I am also suspicious of my method, could you please elaborate or point out a hole. I've put details at :<BR/><BR/>http://www.keyvez.net/2008/05/simulate-7-die-with-5-die-one.htmlAnonymoushttps://www.blogger.com/profile/02708419146799403511noreply@blogger.comtag:blogger.com,1999:blog-17722541.post-60934517821638716902008-05-13T02:18:00.000+02:002008-05-13T02:18:00.000+02:00@keyvez: you have a serious bug in your test progr...@keyvez: you have a serious bug in your test program, as (rand5()+rand5()+rand5()) % 7 theoretically will not give you a uniform distribution.Ariya Hidayathttps://www.blogger.com/profile/03121582140059106015noreply@blogger.comtag:blogger.com,1999:blog-17722541.post-65682011350841527772008-05-13T01:20:00.000+02:002008-05-13T01:20:00.000+02:00I was trying to solve this problem by essentially ...I was trying to solve this problem by essentially doing<BR/><BR/>(rand5()+rand5()+rand5()) % 7<BR/><BR/>Here's the full explanation on my method (with python code):<BR/><BR/>http://www.keyvez.net/2008/05/simulate-7-die-with-5-die-one.htmlAnonymoushttps://www.blogger.com/profile/02708419146799403511noreply@blogger.comtag:blogger.com,1999:blog-17722541.post-8199549715660102962008-02-29T08:41:00.000+01:002008-02-29T08:41:00.000+01:000111 is binary equivalent of 7.so we need to get r...0111 is binary equivalent of 7.<BR/><BR/>so we need to get random numbers for the three bits to get numbers between 0 and 7<BR/>e.g. 000, 001, 010, etc until 111<BR/><BR/>so use rand(5) to determine if u want to set a bit or not...<BR/>if number generated is less than 3, for e.g., u can set the bit. else not set the bit...<BR/><BR/>thus u can generate random number between 0 and 7 by setting each of the three bits independently.<BR/>think about it! d'u really need the rand(5) method for this, or is it just to distract from this solution! :-)<BR/> -shivShivhttps://www.blogger.com/profile/14669282031994586467noreply@blogger.comtag:blogger.com,1999:blog-17722541.post-40781459339650042442007-12-26T16:43:00.000+01:002007-12-26T16:43:00.000+01:00x = rand5();y = round(x * 7 / 5);as we know from m...x = rand5();<BR/>y = round(x * 7 / 5);<BR/><BR/>as we know from math. the distribution of function of random value is equal to function of distribution of random value.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-17722541.post-925037224917646522007-11-12T17:39:00.000+01:002007-11-12T17:39:00.000+01:00Simple solutions FTWint rand7() { return 7 / ran...Simple solutions FTW<BR/><BR/>int rand7() {<BR/> return 7 / rand5();<BR/>}<BR/><BR/>You will only get 1 2 3 or 7 using this solution, but as stated earlier uniformity wasn't requested.<BR/><BR/>More uniform would be<BR/><BR/>int rand7() {<BR/> return (( rand5() + rand5() ) % 7) + 1;<BR/>}Derrick J. Wipplerhttps://www.blogger.com/profile/15279645480670879025noreply@blogger.comtag:blogger.com,1999:blog-17722541.post-5754472214444851902007-11-12T17:01:00.000+01:002007-11-12T17:01:00.000+01:00This comment has been removed by the author.Derrick J. Wipplerhttps://www.blogger.com/profile/15279645480670879025noreply@blogger.com