{"id":801,"date":"2020-04-11T15:03:45","date_gmt":"2020-04-11T20:03:45","guid":{"rendered":"http:\/\/www.moroha.net\/blog\/?p=801"},"modified":"2022-01-16T14:44:47","modified_gmt":"2022-01-16T19:44:47","slug":"imaginary-dice-in-your-head-taking-it-to-the-extreme","status":"publish","type":"post","link":"https:\/\/www.moroha.net\/blog\/archives\/801","title":{"rendered":"Imaginary Dice in Your Head &#8211; Taking it to the Extreme"},"content":{"rendered":"\n<p>Today I saw this <a href=\"https:\/\/boingboing.net\/2020\/04\/10\/how-to-roll-an-imaginary-6-sid.html\">post<\/a> on boingboing with a method to generate a random number from 1 to 6 without the need of a die.  The basic method is this:<\/p>\n\n\n\n<ol><li>Pick a random word, using any method you like.<\/li><li>Sum up the number values of all the letters in the word, with a=1, b=2, etc.<\/li><li>Calculate <code>sum%9<\/code> or <code>mod(sum,9)<\/code> , equivalent to calculating the remainder after dividing the sum by 9.<\/li><li>If the number is 0 or greater than 6, throw it out and go to the next word.<\/li><li>Otherwise, you have your pseudo-random number from 1 to 6.<\/li><\/ol>\n\n\n\n<p>I thought it was pretty interesting, but the first thing I thought of was: why divide by 9?  You&#8217;ll have to throw out about 1\/3 of your numbers because the remainder will be 0, 7, or 8.  Why not divide by 6 instead?  Then you can just use a mapping of 0\u21921, 1\u21922, and so on to 5\u21926 and keep all the numbers without having to throw out anything.  But the <em>real<\/em> challenge is to then take it to the next level and write some code to do it for you.<\/p>\n\n\n\n<p>In the comments very soon somebody posted some results using the words from their <a href=\"https:\/\/en.wikipedia.org\/wiki\/Words_(Unix)\">Words<\/a> file (located at <code>\/usr\/share\/dict\/words<\/code>, it&#8217;s a file with a bunch of words used by spell-checkers in the linux OS) with numbers that looked very good for for the mod(9) and mod(6) versions.<\/p>\n\n\n\n<p>I decided that I could do better than that. Why not pull a bunch of words from the internet and use those?  There&#8217;s no better source of lots of words than free books, and the easiest way to get free books is from <a href=\"https:\/\/www.gutenberg.org\/ebooks\/\">Project Gutenberg<\/a>.<\/p>\n\n\n\n<p>I decided to try writing some code in python, and a quick google search got me some <a href=\"https:\/\/www.nltk.org\/book\/ch03.html\">simple code<\/a> to pull text from any Project Gutenberg book and divide it into a list of individual strings for each word.  After that, it&#8217;s a fairly straightforward process to iterate through each word, use the <code>ord()<\/code> function to get the ASCII value and convert it to an a=1, b=2, etc. number encoding, sum the numbers up to get a total, and then use the <code>mod()<\/code> function to get the remainder by dividing it by 6 or 9. For comparison, I also used the random number generator in python to generate an equal number of random numbers between 1 to 6 as well.<\/p>\n\n\n\n<p>I did that for all the words in Crime and Punishment by Fyodor Dostoevsky and got the following results:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>method               1      2      3      4      5      6\n---------------  -----  -----  -----  -----  -----  -----\nmod(N,6) method  24500  43816  23020  29030  23315  15064\nmod(N,9) method  15804  20820  14081  14960  19924  12867\nrand function    26610  26573  26465  26457  26359  26281<\/code><\/pre>\n\n\n\n<p>Showing as a plot we get the following:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i.imgur.com\/eRKRYq9.png\" alt=\"\"\/><figcaption>Word to Die Number Distribution<\/figcaption><\/figure>\n\n\n\n<p>That&#8217;s actually a <em>really<\/em> bad distribution, there&#8217;s a whole lot more 2&#8217;s and less 6&#8217;s, both for the <code>mod(6)<\/code> an <code>mod(9)<\/code> calculations, but it&#8217;s especially bad for the <code>mod(6)<\/code>, whereas <code>mod(9)<\/code> has too many 2&#8217;s and 5&#8217;s.<\/p>\n\n\n\n<p>Why would that be?  Someone in the comments had a good suggestion: if I&#8217;m just using every word (of three letters or longer, I dropped any one- or two-letter words), then there are probably still a whole lot of <em>the<\/em>&#8216;s and <em>and<\/em>&#8216;s in the list that would throw off my distribution.<\/p>\n\n\n\n<p>Let&#8217;s do a quick calculation and see.  For <em>the<\/em> we have <code>t=20<\/code>,  <code>h=8<\/code>, and <code>e=5<\/code>. That gives a total of <code>20+8+5=33<\/code>.  Dividing 33\/9 gives a remainder of 6, and dividing 33\/6 gives a remainder of 3.  For the mod9 method that number will be dropped, since I just kept results of 0 to 5 (mapping to numbers 1 to 6) and dropped the rest.  For mod9 that number will become 4, using the mapping 3 \u2192 4.<\/p>\n\n\n\n<p>Similarly for <em>and<\/em> we have <code>a=1<\/code>,  <code>n=14<\/code>, and <code>d=4<\/code>. That gives a total of 19, and dividing 19\/9 gives a remainder of 1, and dividing 19\/6 gives the same remainder of 1, since 18 is a common multiple forth 6 and 9.  In both cases this will become a 2 on a die, since we&#8217;re using the mapping 1 \u2192 2.  And looking at the plot, in both cases 2 has the highest number and 4 after that. This is the most likely reason.<\/p>\n\n\n\n<p>So I then fixed the script so that it ignored any word repetitions, so each word is represented only once in the entire word list.  Re-running it gave me the following results for Crime and Punishment:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>method              1     2     3     4     5     6\n ---------------  ----  ----  ----  ----  ----  ----\n mod(N,6) method  1584  1600  1551  1610  1493  1526\n mod(N,9) method  1052  1057  1069  1084  1049  1035\n rand function    1522  1556  1561  1643  1568  1514<\/code><\/pre>\n\n\n\n<p>And as a plot:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i.imgur.com\/PV1vOyf.png\" alt=\"\"\/><figcaption>Word to Die Number Distribution, Improved Algorithm<\/figcaption><\/figure>\n\n\n\n<p>That looks a whole lot better. There are less overall using the mod9 function because it drops about 1\/3 of the numbers because the remainder &gt; 5.  Overall though, it certainly passes the &#8216;looks OK&#8217; test (which is important in science and engineering, don&#8217;t underestimate it), but can we more rigorously determine how random it is?<\/p>\n\n\n\n<p>There is a statistical test we can perform called the Pearson&#8217;s chi-squared test.  Basically how it works is this: if the process is perfectly random, then we can expect to have the exact same number of dice rolls for each number: namely the number of times 1, 2, 3, 4, 5, and 6 should all be the same.  That is our <em>expected value<\/em>.  We take the actual number of times each number is rolled, subtract it from the expected value, square that quantity, and then divide by the expected value.  Then we do the same for all six numbers and sum the result.  This is our chi-squared statistic or  \\(\\chi^2\\) , which should be closer to zero as the randomness of our method increases.  Results from Crime And Punishment:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Method<\/strong><\/td><td> \\(\\mathbf{\\chi^2}\\)<\/td><\/tr><tr><td>mod(6)<\/td><td>6.66<\/td><\/tr><tr><td>mod(9)<\/td><td>1.36<\/td><\/tr><tr><td>rand()<\/td><td>7.81<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The  \\(\\chi^2\\) value from the <code>rand()<\/code> function varies since it uses different random numbers each time, usually it varies from 2 to 7.  This puts the results from the <code>mod(6)<\/code>and the <code>mod(9)<\/code> functions on par with  <br \/>the <code>rand()<\/code> function.<\/p>\n\n\n\n<p>However if we do the same analysis on the results when I kept all the repeated words, we get very different  \\(\\chi^2\\) values:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Method<\/strong><\/td><td> \\(\\mathbf{\\chi^2}\\)<\/td><\/tr><tr><td>mod(6)<\/td><td>17510<\/td><\/tr><tr><td>mod(9)<\/td><td>3184<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n<p><!--EndFragment-->Obviously this is much <em>much<\/em> worse than when we improved the code by removing all repeating words, but quantitatively speaking, how much worse is it.&nbsp; Or in other words, what do those values of 17510 and 5184<br \/>actually mean compared to 6.66 and 1.36?<\/p>\n<p>To answer that we have to look at the chi-squared distribution itself.&nbsp; There is more than you could ever want to learn about it on the wikipedia articles for the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Chi-squared_distribution\">chi-squared distribution<\/a> and the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Pearson%27s_chi-squared_test\">Pearson&#8217;s chi-squared test<\/a>, but basically by using that distribution it allows us to determine values that we can compare our&nbsp;\\(\\chi^2\\) values to in order to see how random our numbers are.<\/p>\n<p>First we define the null hypothesis, which is that our pseudo-random numbers generated using the word-to-number-to-divide-by-number-and keep-the-remainder method are random.&nbsp; We calculate a confidence interval (i.e. 90%, 95%, 99%, 99.99%, etc.) using the chi-squared distribution function for a given number of degrees of freedom (5 in this case, 6 possibilities &#8211; 1) and compare our \\(\\chi^2\\) values to it.&nbsp; If our \\(\\chi^2\\) is greater than the confidence interval, then we reject the null hypothesis and so we can say that our numbers are <em>not<\/em> random to the degree of our confidence.<\/p>\n<p>Let&#8217;s do an example.&nbsp; For 5 degrees of freedom, the value for a confidence interval of 99% is 15.0863.&nbsp; We saw above the the \\(\\chi^2\\) values for using all the repeating words were 17510 and 5184 for the mod6 and mod9 methods respectively.&nbsp; Since both of these values are greater than 15.0863, then we reject the null hypothesis, and so we can say with a 99% confidence that those numbers are <em>not<\/em> random.<\/p>\n<p>However if the numbers are less than our critical value, as is the case when we don&#8217;t repeat numbers, does the converse hold? Can we then say with a 99% confidence that our numbers <em>are<\/em> random?&nbsp; Actually, we can&#8217;t.&nbsp; A hypothesis can never be proven, it can only be <em>disproven.<\/em>&nbsp; So in this case we don&#8217;t reject the hypothesis.&nbsp; We haven&#8217;t proven the numbers <em>are<\/em> random, we simply say that we can&#8217;t prove they <em>aren&#8217;t<\/em> random.<\/p>\n<p>This becomes clearer when we start comparing different confidence values.&nbsp; For the same 5 degrees of freedom, we have the following confidence intervals:<\/p>\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Confidence level<\/strong><\/td><td><strong>Critical<\/strong>   \\(\\mathbf{\\chi^2}\\)  <strong>value<\/strong><\/td><\/tr><tr><td>50%<\/td><td>4.35146<\/td><\/tr><tr><td>90%<\/td><td>9.23636<\/td><\/tr><tr><td>99%<\/td><td>15.0863<\/td><\/tr><tr><td>99.9%<\/td><td>20.515<\/td><\/tr><tr><td>99.99%<\/td><td>25.7448<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>To disprove the null hypothesis with a higher confidence level, that is to have a higher degree of confidence that our numbers are <em>not<\/em> random, requires a higher  \\(\\chi^2\\) value.  So for example if our  \\(\\chi^2=21\\), we are 99.9% confident that the numbers are not random, but we can&#8217;t be 99.99% confident.  There is still a 0.1% chance that the numbers are in fact random, just that by random chance we happened to get a bunch of the same numbers giving us a high  \\(\\chi^2\\) value.  That&#8217;s where the uncertainty is.<\/p>\n\n\n\n<p>If the converse were true, then if our numbers had a  \\(\\chi^2=21\\), then we could say with a 99.99% confidence that our numbers were random, since 21&lt;25.7448.  But because 21&gt;20.515 for the 99.9% confidence interval, we know with at 99.9% confidence that the numbers are in fact <em>not<\/em> random, so also trying to say that we have a 99.99% confidence that they are random contradicts this.  Hence we can never truly prove they are random, we can only prove or fail to prove that they are not random to a given degree of confidence.<\/p>\n\n\n\n<p>However going back to our results, the  \\(\\chi^2\\) values of 17510 and 3184 are much higher than even a 99.9999999999999999% confidence limit.  So we can be pretty much absolutely certain that they are not random.  In fact the highest confidence limit I could even calculate was a confidence level of 99.999&#8230;.(300 9&#8217;s)%, which has a value of about 1400.  We&#8217;re even greater than that!<\/p>\n\n\n\n<p>Anyway, I&#8217;ve added the code I wrote to github <a href=\"https:\/\/github.com\/d3rio\/word-dice-test\">here<\/a>.  Feel free to play with it if you like.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I saw this post on boingboing with a method to generate a random number from 1 to 6 without the need of a die. The basic method is this: Pick a random word, using any method you like. Sum &hellip; <a href=\"https:\/\/www.moroha.net\/blog\/archives\/801\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[15,17,16],"_links":{"self":[{"href":"https:\/\/www.moroha.net\/blog\/wp-json\/wp\/v2\/posts\/801"}],"collection":[{"href":"https:\/\/www.moroha.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.moroha.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.moroha.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.moroha.net\/blog\/wp-json\/wp\/v2\/comments?post=801"}],"version-history":[{"count":18,"href":"https:\/\/www.moroha.net\/blog\/wp-json\/wp\/v2\/posts\/801\/revisions"}],"predecessor-version":[{"id":943,"href":"https:\/\/www.moroha.net\/blog\/wp-json\/wp\/v2\/posts\/801\/revisions\/943"}],"wp:attachment":[{"href":"https:\/\/www.moroha.net\/blog\/wp-json\/wp\/v2\/media?parent=801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.moroha.net\/blog\/wp-json\/wp\/v2\/categories?post=801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.moroha.net\/blog\/wp-json\/wp\/v2\/tags?post=801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}