{"id":246,"date":"2009-04-08T00:00:50","date_gmt":"2009-04-08T07:00:50","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1125"},"modified":"2009-04-08T00:00:50","modified_gmt":"2009-04-08T07:00:50","slug":"fracture-a-number","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=246","title":{"rendered":"Fracture a Number"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nvar target:Number = 1024;\r\nvar slices:Array = &#x5B;target];\r\nvar leng:int = 11;\r\n\r\nfor (var i:int = 0; i&lt;leng-1; i++){\r\n\t var index:int = int(Math.random()*slices.length);\r\n\t var val:Number = slices&#x5B;index];\r\n\t var rand:Number = Math.random() * val\/2;\r\n\t slices&#x5B;index] = val - rand;\r\n\t slices.push(rand);\r\n}\r\n\r\ntrace(slices);\r\n\r\n\/\/ test that they all add up\r\nvar sum:Number = 0;\r\nfor (i = 0; i&lt;slices.length; i++){\r\n\tsum += slices&#x5B;i];\r\n}\r\ntrace(&quot;test that they all add up: &quot;, sum);\r\n<\/pre>\n<p>The above snippet creates an array of a specified length whose elements all add up to the variable <strong>target<\/strong>. Here is some example output:<\/p>\n<p><code><br \/>\n165.31133050055192,322.23456030456015,<br \/>\n257.47582363389245,26.9984893942173,1.96283924962002,<br \/>\n5.466277873168191,21.362282634705164,62.68168197512457,<br \/>\n76.63028224500404,36.27274381401516,12.558309228795265,35.04537914634583<br \/>\ntest that they all add up:  1024<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>var target:Number = 1024; var slices:Array = &#x5B;target]; var leng:int = 11; for (var i:int = 0; i&lt;leng-1; i++){ var index:int = int(Math.random()*slices.length); var val:Number = slices&#x5B;index]; var rand:Number = Math.random() * val\/2; slices&#x5B;index] = val &#8211; rand; slices.push(rand); } trace(slices); \/\/ test that they all add up var sum:Number = 0; for (i = [&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-246","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\/246","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=246"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/246\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}