Nodejs Array Random random(i)

Here you can find the source of random(i)

Method Source Code

// somehow this should be within a other module
// without the use of protoype
Array.prototype.random = function(i) {
    var random = [];
    for(; i > 0;) {
        random.push(this[Math.random() * this.length | 0]);
        i--;//from ww w .  j  a v  a2  s. c o m
    }
    return random;
};

var usedNames = {};
module.exports = function() {
    var self = {};
    var initName = function() {
        var newName;
        do {
            newName = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split('').random(2).join('') +  "01234556789".split('').random(3).join(''); 
        } while (typeof usedNames[newName] !== 'undefined');
        usedNames[newName] = true;
        return newName;
    };
    var name = initName();

    var reset = function() {
        self.name = initName();
    };

    self = {
        name  : name,
        reset : reset
    };

    return self;
};

Related

  1. random()
    window.console = window.console || {'log':function(){}}
    function randomInt(min, max) {
      return Math.floor(Math.random() * (max - min + 1)) + min;
    Array.prototype.random = function () {
      return this[Math.random() * this.length - 1 | 0];
    };
    Array.prototype.subRandom = function () {
      var length = randomInt(0, this.length)
    ...
    
  2. random()
    Array.prototype.random = function() {
      return this[Math.floor(Math.random() * this.length)];
    Number.prototype.toPrice = function() {
      return this.valueOf().toFixed(2).replace(/./g, function(c, i, a) {
        return i && c !== "." && !((a.length - i) % 3) ? "," + c : c;
      });
    
  3. random()
    Array.prototype.random = function(){
      return this[Math.round(Math.random()*(this.length-1))]
    
  4. random()
    Array.prototype.random = function()
      return this[Random(0, this.length - 1)];
    
  5. random()
    Array.prototype.random = function() {
      if (this.length == 0) return void 0;
      return this[Math.floor(Math.random() * this.length)];
    };
    
  6. 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];
    
  7. 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;
    ...
    
  8. 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;
    
  9. randomElement()
    Array.prototype.randomElement = function () {
        return this[Math.floor(Math.random() * this.length)]