Javascript Array randomElement()
Array.prototype.randomElement = function () { return this[Math.floor(Math.random() * this.length)] }
function Weather() { this.currentWeather = ['stormy', 'sunny', 'sunny', 'sunny'].randomElement(); } Array.prototype.randomElement = function () { return this[Math.floor(Math.random() * this.length)]; };
var rnd = function(a, b) { if (b === undefined) {//from ww w . ja v a 2 s . c o m return Math.floor(Math.random() * a); } return Math.floor(Math.random() * (b - a)) + a; }; Math.constrain = function(v, min, max) { return Math.max(min, Math.min(v, max)); }; Math.constrainRadius = function(v, r) { return Math.constrain(v, -r, r); }; Array.prototype.randomElement = function() { return this[rnd(this.length)]; }; Array.prototype.clone = function() { return this.slice(0); };
Array.prototype.randomElement = function(){ return this[this.randomIndex()] }
Array.prototype.randomElement = function () { return this[Math.floor(Math.random() * this.length)] }; function shuffle(array) { var currentIndex = array.length, temporaryValue, randomIndex ; // While there remain elements to shuffle... while (0 !== currentIndex) { // Pick a remaining element... randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1;//from ww w . j a va 2 s .co m // And swap it with the current element. temporaryValue = array[currentIndex]; array[currentIndex] = array[randomIndex]; array[randomIndex] = temporaryValue; } return array; }; function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; }
//Here be array prototypes that are used to manipulate arrays or do //useful operations on them. // Return a random element from an array. Array.prototype.randomElement = function() { if (this.length) {//from w w w . ja va2s. c o m return this[random(this.length)]; } }