Skip to content

Instantly share code, notes, and snippets.

@iansheridan
Created December 9, 2011 19:45
Show Gist options
  • Save iansheridan/1452985 to your computer and use it in GitHub Desktop.
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
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