{"id":2632,"date":"2012-01-05T21:38:33","date_gmt":"2012-01-06T02:38:33","guid":{"rendered":"http:\/\/www.gubatron.com\/blog\/?p=2632"},"modified":"2012-01-05T21:38:33","modified_gmt":"2012-01-06T02:38:33","slug":"javascript-get-n-random-elements-from-a-list","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/javascript-get-n-random-elements-from-a-list\/","title":{"rendered":"javascript: Get N random elements from a List"},"content":{"rendered":"<p>[javascript]<br \/>\n\/**<br \/>\n * Walks linearly through the list to find an element.<br \/>\n * returns true if it&#8217;s found.<br \/>\n *\/<br \/>\nfunction elementIn(collection, element) {<br \/>\n\tfor (var i=0; i &lt; collection.length; i++) {<br \/>\n\t\tif (collection[i]==element) {<br \/>\n\t\t\treturn true;<br \/>\n\t\t}<br \/>\n\t}<br \/>\n\treturn false;<br \/>\n}<\/p>\n<p>\/**<br \/>\n * Returns a new list of n random elements taken out of myList.<br \/>\n *\/<br \/>\nfunction getNElementsAtRandom(myList, n) {<br \/>\n\tvar toGo = n;<br \/>\n\tvar result = [];<br \/>\n\tvar indexesUsed = [];<\/p>\n<p>\twhile (toGo &gt; 0) {<br \/>\n\t\tindex=-1;<\/p>\n<p>\t\tdo {<br \/>\n\t\t\tindex = Math.floor(Math.random()*(myList.length));<br \/>\n\t\t\tconsole.log(index);<br \/>\n\t\t} while (elementIn(indexesUsed, index));<\/p>\n<p>\t\tindexesUsed.push(index);<br \/>\n\t\tresult.push(myList[index]);<br \/>\n\t\ttoGo&#8211;;<br \/>\n\t}<\/p>\n<p>\treturn result;<br \/>\n}<br \/>\n[\/javascript] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[javascript] \/** * Walks linearly through the list to find an element. * returns true if it&#8217;s found. *\/ function elementIn(collection, element) { for (var i=0; i &lt; collection.length; i++) { if (collection[i]==element) { return true; } } return false; } \/** * Returns a new list of n random elements taken out of myList. [&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-2632","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-Gs","jetpack-related-posts":[{"id":2671,"url":"https:\/\/www.gubatron.com\/blog\/how-to-shuffle-a-listarray-in-javascript\/","url_meta":{"origin":2632,"position":0},"title":"How to shuffle a List (or Array) in Javascript","author":"gubatron","date":"February 24, 2012","format":false,"excerpt":"[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>0; i--) { var\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":2047,"url":"https:\/\/www.gubatron.com\/blog\/map-function-in-java\/","url_meta":{"origin":2632,"position":1},"title":"Map function in Java","author":"gubatron","date":"August 31, 2010","format":false,"excerpt":"I read on some email signature something along the lines of: \"If I had a dollar for every for(int i=0; i < size; i++) { ... } I've written I'd be rich\" After coding on Android and learning about some of the tips for performance, like \"With an ArrayList, a\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":2632,"position":2},"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":3386,"url":"https:\/\/www.gubatron.com\/blog\/how-to-make-a-foreach-function-in-javascript\/","url_meta":{"origin":2632,"position":3},"title":"How to make a &#8220;foreach&#8221; function in JavaScript","author":"gubatron","date":"April 4, 2015","format":false,"excerpt":"I thought this would be a simple exercise in case of having to interview someone for a JavaScript position. \"How would you make your own 'foreach' in JavaScript\" I came up with the following solution: \/\/ \/\/ collection: A list of objects. \/\/ onElementIterationCallback: The function to be called on\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":341,"url":"https:\/\/www.gubatron.com\/blog\/floatcenter\/","url_meta":{"origin":2632,"position":4},"title":"float:center","author":"gubatron","date":"July 12, 2006","format":false,"excerpt":"You wish... So many work arounds to center divs, that I was almost getting to the point where I was about to start selling black t-shirts with the words float:center If you're a web developer, web designer, and you have to deal with CSS, and CSS not working properly on\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":3,"url":"https:\/\/www.gubatron.com\/blog\/javascript-quicksort-implementation-with-dynamic-comparator\/","url_meta":{"origin":2632,"position":5},"title":"Javascript Quicksort implementation with dynamic comparator.","author":"gubatron","date":"February 29, 2012","format":false,"excerpt":"[javascript] Array.prototype.swap=function(a, b) { var tmp=this[a]; this[a]=this[b]; this[b]=tmp; } function quickSort(array,comparator) { qsort(array,0,array.length,comparator); } \/** * NOTE: the comparator is a dynamic function you will define like so comparator(a,b) { if (a > b) return 1; else if (a < b) return -1; else { return 0; } } *\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":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/2632","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=2632"}],"version-history":[{"count":0,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/2632\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=2632"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=2632"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=2632"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}