{"id":204,"date":"2009-02-27T09:41:24","date_gmt":"2009-02-27T16:41:24","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=863"},"modified":"2009-02-27T09:41:24","modified_gmt":"2009-02-27T16:41:24","slug":"random-sort","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=204","title":{"rendered":"Random Sort"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nfunction randomize(a:*, b:*):int{\r\n       return Math.round(Math.random()*8) - 4;\r\n}\r\n\r\nvar i:int;\r\nvar fruits:Array;\r\n\r\ntrace(&quot;Math.random()&quot;);\r\nfor (i = 0; i&lt;4; i++){\r\n  \/\/ reset fruits array:\r\n  fruits = &#x5B;&quot;apple&quot;, &quot;grape&quot;,&quot;pear&quot;,&quot;cherry&quot;];\r\n  fruits.sort(randomize);\r\n  trace(fruits);\r\n}\r\n\r\n\r\n\/\/ seeds\r\nvar s1:Number= 0xFF00FF;\r\nvar s2:Number = 0xCCCCCC;\r\nvar s3:Number= 0xFF00F0;\r\n\r\nfunction tRandomize(a:*, b:*):int{\r\n       return Math.round(rand()*8) - 4;\r\n}\r\n\r\ntrace(&quot;nTausworthe rand()&quot;);\r\nfor (i= 0; i&lt;4; i++){\r\n  fruits = &#x5B;&quot;apple&quot;, &quot;grape&quot;,&quot;pear&quot;,&quot;cherry&quot;];\r\n  fruits.sort(tRandomize);\r\n  trace(fruits);\r\n}\r\n\/\/ from www.ams.org\/mcom\/1996-65-213\/S0025-5718-96-00696-5\/S0025-5718-96-00696-5.pdf\r\nfunction rand():Number {\r\n\ts1=((s1&amp;4294967294)&lt;&lt;12)^(((s1&lt;&lt;13)^s1)&gt;&gt;19);\r\n\ts2=((s2&amp;4294967288)&lt;&lt;4)^(((s2&lt;&lt;2)^s2)&gt;&gt;25);\r\n\ts3=((s3&amp;4294967280)&lt;&lt;17)^(((s3&lt;&lt;3)^s3)&gt;&gt;11);\r\n\tvar r:Number = (s1^s2^s3) * 2.3283064365e-10;\r\n\tr = (r&lt;0) ? r+=1 : r;\r\n\treturn r;\r\n}\r\n\r\n\/*\r\noutputs:\r\nMath.random()\r\ngrape,apple,pear,cherry\r\npear,cherry,apple,grape\r\ngrape,apple,pear,cherry\r\ngrape,apple,cherry,pear\r\n\r\nTausworthe rand()\r\napple,grape,pear,cherry\r\ncherry,grape,pear,apple\r\ngrape,apple,cherry,pear\r\ngrape,pear,apple,cherry\r\n*\/\r\n<\/pre>\n<p>The above shows how to randomly sort  or shuffle an array. This is useful in games. To achieve this I made use of the compareFunction argument of Array.sort(). Most sorting algorithms go through the array and compare values until the desired sort order is achieved.  The compareFunction argument is a function that takes two values a and b and returns an integer that is negative positive or zero&#8230; see this info from the docs:<br \/>\n    * A negative return value specifies that A appears before B in the sorted sequence.<br \/>\n    * A return value of 0 specifies that A and B have the same sort order.<br \/>\n    * A positive return value specifies that A appears after B in the sorted sequence.<\/p>\n<p>So in the case of a randomizing an array you simply need to return a random int -1, 0 or 1. This is what I&#8217;ve done in the past (Math.round()*2) -1) &#8230; but when I was writing this snippet it seemed like 0 caused less variation in the output of the array so I made the range from -4 to 4 instead. This could have just been my imagination, but it seems like having less chance of a zero caused the arrays to be a bit more shuffled. <\/p>\n<p>The reason I also included a version that uses Tausworthe is because of the easy seeding. In some cases you may want to use seeded randomness to sort an array.<\/p>\n<p>UPDATE:<br \/>\nWas digging around about this and found a much faster method for randomizing arrays&#8230; not a big deal if you have small arrays, but if you need to randomize 1000&#8217;s of values <a href=\"http:\/\/mrsteel.wordpress.com\/2007\/06\/15\/randomize-array-shuffle-an-array-in-flash\/\">this method<\/a> is much faster than using Array.sort() <\/p>\n","protected":false},"excerpt":{"rendered":"<p>function randomize(a:*, b:*):int{ return Math.round(Math.random()*8) &#8211; 4; } var i:int; var fruits:Array; trace(&quot;Math.random()&quot;); for (i = 0; i&lt;4; i++){ \/\/ reset fruits array: fruits = &#x5B;&quot;apple&quot;, &quot;grape&quot;,&quot;pear&quot;,&quot;cherry&quot;]; fruits.sort(randomize); trace(fruits); } \/\/ seeds var s1:Number= 0xFF00FF; var s2:Number = 0xCCCCCC; var s3:Number= 0xFF00F0; function tRandomize(a:*, b:*):int{ return Math.round(rand()*8) &#8211; 4; } trace(&quot;nTausworthe rand()&quot;); for (i= 0; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,94],"tags":[3,4],"class_list":["post-204","post","type-post","status-publish","format-standard","hentry","category-arrays","category-misc","tag-actionscript","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=204"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/204\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}