Nodejs Array Get Random getRandomItems(count)

Here you can find the source of getRandomItems(count)

Method Source Code

"use strict";/*from   w  w w.  j  a v  a  2s.c om*/


Array.prototype.getRandomItems = function(count) {
   var items = [];
   for (var i = 0; i < count; i++) {
      if (this.length >= 1) {
         items.push(this[NLib.math.randomize(0, this.length - 1)]);
      } else {
         items.push(this[0]);
      }
   }
   return items;
};


Array.prototype.getRandomItem = function() {
   return this.getRandomItems(1);
}

Related

  1. getRandom()
    Array.prototype.getRandom = function(){
      var result = this[Math.floor(Math.random()*this.length)];
      return result;
    
  2. getRandomArrayItem()
    function shuffleArray(array) {
      for (var i = array.length - 1; i > 0; i--) {
        var j = Math.floor(Math.random() * (i + 1));
        var temp = array[i];
        array[i] = array[j];
        array[j] = temp;
      return array;
    Array.prototype.getRandomArrayItem = function() {
      return this[Math.floor(Math.random()*this.length)];
    
  3. getRandomElement()
    Array.prototype.getRandomElement = function() {
      var i = getRandBetween(0, this.length-1);
      return this[i];
    function getRandBetween(lo, hi) {
      return parseInt(Math.floor(Math.random()*(hi-lo+1))+lo);
    
  4. getRandomIndex()
    Array.prototype.getRandomIndex = function(){
        return Math.floor(Math.random()*this.length);
    
  5. getRandomItem()
    Array.prototype.getRandomItem = function(){
        var index = Math.floor(Math.random() * this.length);
        return this[index];
    };
    
  6. getRandomValue()
    Array.prototype.getRandomValue = function(){
        return this[Math.floor(Math.random()*this.length)];