{"id":195,"date":"2009-02-19T10:40:53","date_gmt":"2009-02-19T17:40:53","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=809"},"modified":"2009-02-19T10:40:53","modified_gmt":"2009-02-19T17:40:53","slug":"tausworthe-random-numbers-seeded","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=195","title":{"rendered":"Tausworthe Random Numbers (seeded)"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n\/\/ best thing about this are the random seeds\r\nvar s1:Number= 0xFFFFFF;\r\nvar s2:Number = 0xCCCCCC;\r\nvar s3:Number= 0xFF00FF;\r\n\r\n\/\/ saw this algorithm on this somewhat annoying thread:\r\n\/\/ http:\/\/www.reddit.com\/r\/programming\/comments\/7yjlc\/why_you_should_never_use_rand_plus_alternative\/\r\n\/\/ additional googling brought me to this: http:\/\/wakaba.c3.cx\/soc\/kareha.pl\/1100499906\/\r\n\/\/ and finally to this ( didn&#039;t understand most of this one) www.ams.org\/mcom\/1996-65-213\/S0025-5718-96-00696-5\/S0025-5718-96-00696-5.pdf\r\n\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\/\/ see a visual comparison between this and actionscript&#039;s Math.random()\r\n\r\nvar canvas:BitmapData = new BitmapData(400,400,false, 0xCCCCCC);\r\naddChild(new Bitmap(canvas));\r\n\r\nvar posX:Number;\r\nvar posY:Number;\r\n\r\naddEventListener(Event.ENTER_FRAME, onLoop);\r\n\r\nfunction onLoop(evt:Event):void {\r\n\r\n\tfor (var i:int = 0; i&lt;100; i++){\r\n\t\tposX = rand() * 180\r\n\t\tposY = rand() * 180\r\n\t\tcanvas.setPixel( 100 + posX, 10 + posY, 0x000000);\r\n\t}\r\n\t\r\n\tfor (i = 0; i&lt;100; i++){\r\n\t\tposX = Math.random() * 180\r\n\t\tposY = Math.random() * 180\r\n\t\tcanvas.setPixel( 100 + posX, 210 + posY, 0x000000);\r\n\t}\r\n}\r\n<\/pre>\n<p>The above snippet demo&#8217;s an alternative random number generator that uses the <a href=\"http:\/\/www.cs.dartmouth.edu\/~akapadia\/project2\/node9.html\">Tausworthe<\/a> method. <\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.actionsnippet.com\/imgs\/rng.jpg\" \/><br \/>\n<i>Top square is Tausworthe and bottom is Math.random()<\/i><\/p>\n<p>I don&#8217;t actually know much about this, or fully understand how it works. I just saw it on a random reddit thread, googled about it for a few minutes and then ported it to actionscript. <\/p>\n<p>There&#8217;s lots of talk about what the BEST random number generator is&#8230;. which one is the fastest, which one is the most random. etc&#8230; I don&#8217;t really know much about that, especially since I&#8217;m just playing around with code and I don&#8217;t need a RNG for some scientific experiment. For me, what&#8217;s nice about this approach is the three random seeds. For some reason, Math.random() doesn&#8217;t have a place where you can enter a seed for random numbers. Random seeds are very useful when you want to do something random but want to be able to replicate the random results at a later time. For instance:<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nvar s1:Number= 0xFFFFFF;\r\nvar s2:Number = 0xCCCCCC;\r\nvar s3:Number= 0xFF00FF;\r\n\r\ntrace(&quot;first three values:&quot;);\r\ntrace(rand());\r\ntrace(rand())\r\ntrace(rand())\r\n\r\ns1 = 0xFF;\r\ns2 = 0xEFEFEF;\r\ns3 = 19008;\r\n\r\ntrace(&quot;nfirst three values with different seeds:&quot;);\r\ntrace(rand());\r\ntrace(rand())\r\ntrace(rand())\r\n\r\ns1 = 0xFFFFFF;\r\ns2  = 0xCCCCCC;\r\ns3 = 0xFF00FF;\r\n\r\ntrace(&quot;noriginal three values:&quot;);\r\ntrace(rand());\r\ntrace(rand())\r\ntrace(rand())\r\n\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\nfirst three values:\r\n0.051455706589931385\r\n0.050584114155822715\r\n0.417276361484596\r\n\r\nfirst three values with different seeds:\r\n0.6032885762463134\r\n0.9319786790304015\r\n0.8631882804934321\r\n\r\noriginal three values:\r\n0.051455706589931385\r\n0.050584114155822715\r\n0.417276361484596\r\n*\/\r\n<\/pre>\n<p>I stumbled upon a bunch of other random number algorithms, maybe I&#8217;ll throw them in a class in the next couple days.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/\/ best thing about this are the random seeds var s1:Number= 0xFFFFFF; var s2:Number = 0xCCCCCC; var s3:Number= 0xFF00FF; \/\/ saw this algorithm on this somewhat annoying thread: \/\/ http:\/\/www.reddit.com\/r\/programming\/comments\/7yjlc\/why_you_should_never_use_rand_plus_alternative\/ \/\/ additional googling brought me to this: http:\/\/wakaba.c3.cx\/soc\/kareha.pl\/1100499906\/ \/\/ and finally to this ( didn&#039;t understand most of this one) www.ams.org\/mcom\/1996-65-213\/S0025-5718-96-00696-5\/S0025-5718-96-00696-5.pdf function rand():Number { s1=((s1&amp;4294967294)&lt;&lt;12)^(((s1&lt;&lt;13)^s1)&gt;&gt;19); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[94,41],"tags":[3,4],"class_list":["post-195","post","type-post","status-publish","format-standard","hentry","category-misc","category-number","tag-actionscript","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/195","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=195"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/195\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}