Skip to content

Instantly share code, notes, and snippets.

@samyakbhuta
Created May 17, 2011 06:29
Show Gist options
  • Save samyakbhuta/976044 to your computer and use it in GitHub Desktop.
Save samyakbhuta/976044 to your computer and use it in GitHub Desktop.
randomInArray
/*
* Returns a random element in a function.
*/
var randomInArray = function (anArray){
//TODO : Check if anArray is really an array.
var randomIndex=Math.floor(Math.random()*anArray.length)
return anArray[randomIndex];
}
//Usage
myArray = ["a","b","c","d"];
randomInArray(myArray); // Will be any of "a","b","c" or "d"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment