Nodejs Array Random randomElement()

Here you can find the source of randomElement()

Method Source Code

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 av a2 s  . c  o 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;
}

Related

  1. random(last)
    Array.prototype.random = function(last) {
        var num = 0;
        do {
           num = Math.floor(Math.random() * this.length);
        } while (this[num] == last);
        return this[num];
    
  2. random(nelm)
    Array.prototype.random = function(nelm) {
      nelm = nelm || 1;
      if (nelm <= 0) {
        nelm = 1;
      if (nelm === 1) {
        return this[Math.floor(Math.random() * this.length)];
      var ret = [], i;
    ...
    
  3. random(num, enableDuplicate)
    Array.prototype.random = function(num, enableDuplicate){
      var array = [];
      var duplicate;
      if(typeof duplicate === "undefined"){
        duplicate = false;
      else{
        duplicate = enableDuplicate;
      if(num <= this.length){
        var index;
        for(var i=0; i<num; i++){
          index = Math.floor(Math.random()*this.length);
          array.push(this[index]); 
          if(!enableDuplicate){
            this.splice(index, 1);
        return array;
    module.exports = Array;
    
  4. randomElement()
    Array.prototype.randomElement = function () {
        return this[Math.floor(Math.random() * this.length)]
    
  5. randomElement()
    function Weather() {
      this.currentWeather = ['stormy', 'sunny', 'sunny', 'sunny'].randomElement();
    Array.prototype.randomElement = function () {
      return this[Math.floor(Math.random() * this.length)];
    };
    
  6. randomPick()
    Array.prototype.randomPick = function () {
        return this[_.random(this.length - 1)];
    };
    
  7. randomPick()
    Array.prototype.randomPick = function () {
        return this[Math.floor(Math.random()*this.length)];
    };
    
  8. randomPick()
    Array.prototype.randomPick = function() {
      var index = Math.floor(Math.random() * this.length);
      return this[index];
    
  9. randomRemove()
    Array.prototype.randomRemove = function () {
      return this.splice(Math.floor(Math.random() * this.length), 1)[0];