{"id":107,"date":"2006-07-28T15:03:28","date_gmt":"2006-07-28T19:03:28","guid":{"rendered":"http:\/\/www.rakkar.org\/blog\/?p=107"},"modified":"2006-07-28T15:03:28","modified_gmt":"2006-07-28T19:03:28","slug":"how-to-randomize-a-list-in-order","status":"publish","type":"post","link":"https:\/\/rakkar.org\/blog\/index.php\/2006\/07\/28\/how-to-randomize-a-list-in-order\/","title":{"rendered":"How to randomize a list in order"},"content":{"rendered":"<p>\t\t\t\tI don&#8217;t remember the link but there was a gambling site a while back that published their randomization algorithm in an attempt to show how fair they were.  It went as follows:<\/p>\n<p>for each element in list<br \/>\n  pick position in the entire list to put this element in the list.<br \/>\n    swap elements with the current and picked position<\/p>\n<p>The problem with that is that items at the front of the list will be moved more times on average than items at the end of this list.   This is because items which are moved earlier (items at the front of the list) have more chance to be moved past the current index into the list, thus being picked to be moved again.  This distorts the randomization curve such that items at the end of the list are more likely to stay there.<\/p>\n<p>As a result, they got hacked.<\/p>\n<p>Here&#8217;s the proper way to randomly sort a list in place:<\/p>\n<p><code><br \/>\nvoid SystemAddressList::RandomizeOrder(void)<br \/>\n{<br \/>\n\tunsigned index, size, randIndex;<br \/>\n\tPlayerID temp;<br \/>\n\tsize = systemList.Size();<br \/>\n\tfor (index=0; index < size; index++)\n\t{\n\t\trandIndex=index + (randomMT() % (size-index));\n\t\tif (randIndex!=index)\n\t\t{\n\t\t\ttemp=systemList[index];\n\t\t\tsystemList[index]=systemList[randIndex];\n\t\t\tsystemList[randIndex]=temp;\n\t\t}\t\t\n\t}\n}\n<\/code><\/code>\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I don&#8217;t remember the link but there was a gambling site a while back that published their randomization algorithm in an attempt to show how fair they were. It went as follows: for each element in list pick position in the entire list to put this element in the list. swap elements with the current [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/posts\/107"}],"collection":[{"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=107"}],"version-history":[{"count":0,"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/posts\/107\/revisions"}],"wp:attachment":[{"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}