Skip to content

Instantly share code, notes, and snippets.

@Samstiles
Created January 7, 2014 20:46
Show Gist options
  • Save Samstiles/8306594 to your computer and use it in GitHub Desktop.
Save Samstiles/8306594 to your computer and use it in GitHub Desktop.
Select random element in an array (array prototype function)
Array.prototype.randomElement = function () {
return this[Math.floor(Math.random() * this.length)]
}
@Samstiles
Copy link
Author

Usage:

var array = ["elem1", "elem2", "elem3"],
randomElement = array.randomElement();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment