Created
December 9, 2011 19:45
-
-
Save iansheridan/1452985 to your computer and use it in GitHub Desktop.
sample of selecting 5 random items from an array in javascript without any duplications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","aa","bb","cc","dd"]; | |
b = []; | |
while(b.length<5){ | |
t = Math.round(Math.random() * (a.length-1)); | |
console.log(t,$.inArray(a[t],b)); | |
if($.inArray(a[t],b) == -1){ | |
b.push(a[t]); | |
} | |
} | |
console.log(b[0]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment