Nodejs Array Random random(nelm)

Here you can find the source of random(nelm)

Method Source Code

Array.prototype.random = function(nelm) {
  nelm = nelm || 1;//from   ww  w . j a v a 2s .  c o  m
  if (nelm <= 0) {
    nelm = 1;
  }
  if (nelm === 1) {
    return this[Math.floor(Math.random() * this.length)];
  }
  var ret = [], i;
  for (i = 0; i < nelm; i++) {
    ret.push(this[Math.floor(Math.random() * this.length)]);
  }
  return ret;
};

Related

  1. random()
    Array.prototype.random = function(){
      return this[Math.round(Math.random()*(this.length-1))]
    
  2. random()
    Array.prototype.random = function()
      return this[Random(0, this.length - 1)];
    
  3. random()
    Array.prototype.random = function() {
      if (this.length == 0) return void 0;
      return this[Math.floor(Math.random() * this.length)];
    };
    
  4. random(i)
    Array.prototype.random = function(i) {
        var random = [];
        for(; i > 0;) {
            random.push(this[Math.random() * this.length | 0]);
            i--;
        return random;
    };
    var usedNames = {};
    ...
    
  5. 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];
    
  6. 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;
    
  7. randomElement()
    Array.prototype.randomElement = function () {
        return this[Math.floor(Math.random() * this.length)]
    
  8. randomElement()
    function Weather() {
      this.currentWeather = ['stormy', 'sunny', 'sunny', 'sunny'].randomElement();
    Array.prototype.randomElement = function () {
      return this[Math.floor(Math.random() * this.length)];
    };
    
  9. randomElement()
    Array.prototype.randomElement = function () {
        return this[Math.floor(Math.random() * this.length)]
    };
    function shuffle(array) {
      var currentIndex = array.length, temporaryValue, randomIndex ;
      while (0 !== currentIndex) {
        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex -= 1;
        temporaryValue = array[currentIndex];
    ...