{"id":112,"date":"2010-12-07T02:08:55","date_gmt":"2010-12-07T06:08:55","guid":{"rendered":"http:\/\/www.joebillman.com\/blog\/?p=112"},"modified":"2010-12-07T02:08:55","modified_gmt":"2010-12-07T06:08:55","slug":"random-vector-and-random-array-functions","status":"publish","type":"post","link":"https:\/\/www.joebillman.com\/blog\/?p=112","title":{"rendered":"Random Vector and Random Array Functions"},"content":{"rendered":"<p>I&#8217;ve been working on a Blackjack game and have needed to do a lot of shuffling, cards that is. I&#8217;ve seen quite a few shuffling functions over the years. Here is the one I like, partly because I wrote it. Well okay I took what I liked in the others and modified them to come up with these:<\/p>\n<pre style=\"background-color: #e5e5e5;\">public static function randomArray(src:Array):void\n{\n    var len:uint = src.length;\n    for(var i:int=len-1; i&gt;=0; i--)\n    {\n        var randIndex:uint = randomUint(0, len-1);\n        var temp:* = src[randIndex];\n        src[randIndex] = src[i];\n        src[i] = temp;\n    }\n}<\/pre>\n<p>Vector version looks like this:<\/p>\n<pre style=\"background-color: #e5e5e5;\">public static function randomVector(src:*):void\n{\n    var len:uint = src.length;\n    for(var i:int=len-1; i&gt;=0; i--)\n    {\n        var randIndex:uint = randomUint(0, len-1);\n        var temp:* = src[randIndex];\n        src[randIndex] = src[i];\n        src[i] = temp;\n    }\n}<\/pre>\n<p>Here is a sample of how the vector version is used:<\/p>\n<pre style=\"background-color: #e5e5e5;\">Randomize.randomVector(cards as Vector.&lt;Card&gt;);<\/pre>\n<p>Here is the randomUint function that it uses:<\/p>\n<pre style=\"background-color: #e5e5e5;\">public static function randomUint(min:uint, max:uint):uint\n{\n    return Math.random()*((max+1)-min)+min;\n}<\/pre>\n<p>I like to call these functions a random number of times so that the deck of cards is nice and shuffled. Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been working on a Blackjack game and have needed to do a lot of shuffling, cards that is. I&#8217;ve seen quite a few shuffling functions over the years. Here is the one I like, partly because I wrote it. Well okay I took what I liked in the others and modified them to come &hellip; <a href=\"https:\/\/www.joebillman.com\/blog\/?p=112\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Random Vector and Random Array Functions&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,26,27],"tags":[],"class_list":["post-112","post","type-post","status-publish","format-standard","hentry","category-actionscript","category-problem-solved","category-programming"],"_links":{"self":[{"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/112","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=112"}],"version-history":[{"count":0,"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/112\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}