{"id":328,"date":"2009-06-27T05:00:01","date_gmt":"2009-06-27T12:00:01","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1640"},"modified":"2009-06-27T05:00:01","modified_gmt":"2009-06-27T12:00:01","slug":"mathrandom-mathrandom","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=328","title":{"rendered":"Math.random() * Math.random()"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nvar rand:Number = Math.random() * Math.random() * Math.random();\r\n<\/pre>\n<p>This is a trick I use when I need more contrast in my random numbers. In this case, the variable rand will get closer to the number 1 significantly less frequently than if you just used Math.random() once. <\/p>\n<p>To illustrate this I created this snippet:<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n&#x5B;SWF(width=800, height=600)]\r\n\r\nvar r:Number = Math.random() * Math.random() * Math.random();\r\nvar inc:int = 0;\r\nvar xp:Number = 10;\r\nvar yp:Number = 10;\r\nvar count:int = 1;\r\n\r\nvar compare:Shape = Shape(addChild(new Shape()));\r\ncompare.graphics.lineStyle(0,0x2222FF);\r\n\r\ngraphics.lineStyle(0,0x00000);\r\nscaleX = scaleY = 2;\r\n\r\naddEventListener(Event.ENTER_FRAME, onLoop);\r\nfunction onLoop(evt:Event):void {\r\n\t\r\n     r =  Math.random() * Math.random() * Math.random();\r\n\r\n\t if (inc == 0){\r\n\t\t graphics.moveTo(xp + inc, yp + 30 - r * 30);\r\n\t }else{\r\n\t\t graphics.lineTo(xp + inc, yp + 30 - r * 30);\r\n\t }\r\n\t \r\n\t r = Math.random();\r\n\t  if (inc == 0){\r\n\t\t compare.graphics.moveTo(xp + inc, yp + 70 - r * 30);\r\n\t }else{\r\n\t\t compare.graphics.lineTo(xp + inc, yp + 70 - r * 30);\r\n\t }\r\n\t inc++;\r\n\t if (inc == 50){\r\n\t\t inc = 0;\r\n\t\t xp = 10 + count % 6 * 60;\r\n\t\t yp = 10 + int(count \/ 6) * 80;\r\n\t\t r = Math.random()*Math.random();\r\n\t\t count++;\r\n\t }\r\n}\r\n<\/pre>\n<p>The blue lines plots normal Math.random() and the black lines plots Math.random()*Math.random()*Math.random()<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/actionsnippet.com\/wp-content\/rand_contrast.gif\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>var rand:Number = Math.random() * Math.random() * Math.random(); This is a trick I use when I need more contrast in my random numbers. In this case, the variable rand will get closer to the number 1 significantly less frequently than if you just used Math.random() once. To illustrate this I created this snippet: &#x5B;SWF(width=800, height=600)] [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37,23],"tags":[3,14,4],"class_list":["post-328","post","type-post","status-publish","format-standard","hentry","category-one-liners","category-random","tag-actionscript","tag-as3","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/328","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=328"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/328\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=328"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=328"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=328"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}