{"id":2671,"date":"2012-02-24T03:19:43","date_gmt":"2012-02-24T03:19:43","guid":{"rendered":"http:\/\/www.gubatron.com\/blog\/?p=2671"},"modified":"2012-02-28T04:54:38","modified_gmt":"2012-02-28T04:54:38","slug":"how-to-shuffle-a-listarray-in-javascript","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/how-to-shuffle-a-listarray-in-javascript\/","title":{"rendered":"How to shuffle a List (or Array) in Javascript"},"content":{"rendered":"<p>[javascript]<br \/>\n\/**<br \/>\n * Returns number starting from offset up to n-1<br \/>\n *\/<br \/>\nfunction getRandomWithOffset(n,offset) {<br \/>\n  return Math.floor(Math.random()*n+offset);<br \/>\n}<\/p>\n<p>\/**<br \/>\n * Returns random integer from 0 to n-1<br \/>\n *\/<br \/>\nfunction getRandom(n) {<br \/>\n    return getRandomWithOffset(n,0);<br \/>\n}<\/p>\n<p>\/** Fisher\u2013Yates shuffle algorithm O(n) *\/<br \/>\nfunction shuffleList(list) {<br \/>\n    for (var i=list.length-1; i&gt;0; i&#8211;) {<br \/>\n        var j = getRandom(i+1);<br \/>\n        var buffer = list[i];<br \/>\n        list[i]=list[j];<br \/>\n        list[j]=buffer;<br \/>\n    }<br \/>\n}<\/p>\n<p>\/** Usage example:<br \/>\nvar myList = [1,2,3,4,5,6,7,8,9];<br \/>\nshuffleList(myList); \/\/after this the list has been shuffled.<br \/>\n*\/<br \/>\n[\/javascript]<\/p>\n<p>Note: This is a simple linear algorithm to shuffle. If you need to do some serious shuffling, say you&#8217;re creating a poker game or something where it&#8217;s crucial where nobody can predict the order of the cards DO NOT USE this algorithm, go for something <a href=\"http:\/\/en.wikipedia.org\/wiki\/Shuffling#Shuffling_techniques\">more complex<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[javascript] \/** * Returns number starting from offset up to n-1 *\/ function getRandomWithOffset(n,offset) { return Math.floor(Math.random()*n+offset); } \/** * Returns random integer from 0 to n-1 *\/ function getRandom(n) { return getRandomWithOffset(n,0); } \/** Fisher\u2013Yates shuffle algorithm O(n) *\/ function shuffleList(list) { for (var i=list.length-1; i&gt;0; i&#8211;) { var j = getRandom(i+1); var buffer [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[15],"tags":[],"class_list":["post-2671","post","type-post","status-publish","format-standard","hentry","category-code"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-H5","jetpack-related-posts":[{"id":2632,"url":"https:\/\/www.gubatron.com\/blog\/javascript-get-n-random-elements-from-a-list\/","url_meta":{"origin":2671,"position":0},"title":"javascript: Get N random elements from a List","author":"gubatron","date":"January 5, 2012","format":false,"excerpt":"[javascript] \/** * Walks linearly through the list to find an element. * returns true if it's found. *\/ function elementIn(collection, element) { for (var i=0; i < collection.length; i++) { if (collection[i]==element) { return true; } } return false; } \/** * Returns a new list of n random\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2267,"url":"https:\/\/www.gubatron.com\/blog\/nsis-strcontains-function-find-the-index-of-a-sub-string\/","url_meta":{"origin":2671,"position":1},"title":"NSIS: StrContains function &#8211; Find the index of a sub string.","author":"gubatron","date":"August 31, 2011","format":false,"excerpt":"Recently I was out of internet and I needed to implement a function that would search if a String was part of another String for an NSIS installer. Here's my rendition of such a function. Once I came back online I found out about StrStr, but here's another option that\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1535,"url":"https:\/\/www.gubatron.com\/blog\/javascript-get-how-many-digits-are-there-in-a-decimal-number\/","url_meta":{"origin":2671,"position":2},"title":"JavaScript: Get how many digits are there in a decimal number","author":"gubatron","date":"January 16, 2010","format":false,"excerpt":"\/** * 1 + Floor(LogBase10(number)) * *\/ function digits(n) { return 1+Math.floor(Math.log(n)\/Math.log(10)); } Yes, javascript doesn't have a Math.log10() function. I tried doing it by dividing by Math.ln10 constant but the function won't return the correct number of digits when you pass numbers like 100,1000,10000,etc. So I just divided by\u2026","rel":"","context":"In &quot;Geeklife&quot;","block_context":{"text":"Geeklife","link":"https:\/\/www.gubatron.com\/blog\/category\/geeklife\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2634,"url":"https:\/\/www.gubatron.com\/blog\/javascript-format-a-number-to-display-thousands-us-style\/","url_meta":{"origin":2671,"position":3},"title":"javascript: Format a number to display thousands US style (#,###,###,###)","author":"gubatron","date":"January 5, 2012","format":false,"excerpt":"[javascript] \/** * Format a number to display thousands like in the US -> 1000000 => 1,000,000 * @param number * @returns *\/ function formatThousands(number) { return Math.max(0, number).toFixed(0).replace(\/(?=(?:d{3})+$)(?!^)\/g, ','); } [\/javascript]","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":799,"url":"https:\/\/www.gubatron.com\/blog\/function-callbacks-in-c\/","url_meta":{"origin":2671,"position":4},"title":"Function callbacks in C","author":"gubatron","date":"May 2, 2008","format":false,"excerpt":"Ever since I started programming in Javascript, and doing asynchronous function calls, I've found myself to be addicted to passing functions as parameters. I do it a lot in python and php, it's very easy to do this on all these dynamic typed languages. I never had this concept of\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2063,"url":"https:\/\/www.gubatron.com\/blog\/android-programming-how-to-obtain-the-wifis-corresponding-networkinterface\/","url_meta":{"origin":2671,"position":5},"title":"Android: How to obtain the WiFi&#8217;s corresponding NetworkInterface","author":"gubatron","date":"September 19, 2010","format":false,"excerpt":"Let's say for some odd reason in the world you do need to get the corresponding NetworkInterface object of the Wifi on your android, in my case I needed to have my WiFi device send multicast packets, and I wanted my MulticastSocket to only send packets through the WiFi device\u2026","rel":"","context":"In &quot;Android&quot;","block_context":{"text":"Android","link":"https:\/\/www.gubatron.com\/blog\/category\/android\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/2671","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/comments?post=2671"}],"version-history":[{"count":8,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/2671\/revisions"}],"predecessor-version":[{"id":2685,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/2671\/revisions\/2685"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=2671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=2671"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=2671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}