Nodejs Array Pick pick()

Here you can find the source of pick()

Method Source Code

"use strict";//w w  w  . j a  v  a2 s. c o  m

var fs = require("fs"),
    path = require("path");

// http://dhtmlexamples.com/2012/04/12/parsing-text-files-using-node-js/

var charfile = process.argv[2];
var dialogfile = process.argv[3];
var output = process.argv[4];

if (!charfile || !dialogfile || !output) {
    console.error("must provide both character, dialog and output files as params!");
    process.exit(1);
}


Array.prototype.pick = function() {
    return this[Math.floor(Math.random() * this.length)];
};

var c = JSON.parse(fs.readFileSync(charfile, "ascii"));
var d = JSON.parse(fs.readFileSync(dialogfile, "ascii"));

var chars = c.characters,
    dialogs = d.dialogue,
    newlines = [], i;

for (i = 0; i < 100; i++) {
    newlines.push(chars.pick() + ": " + dialogs.pick());
}

fs.writeFile(output, newlines.join("\n"));

Related

  1. pick()
    Array.prototype.pick = function(){
        var randomIndex = Math.round(Math.random() * (this.length-1))
        return this[randomIndex]
    
  2. pick()
    Array.prototype.pick = function () {
      'use strict';
      if (!this.length) {
        return null;
      return this.splice(ROT.RNG.getUniformInt(0, this.length), 1)[0];
    };
    
  3. pick()
    Array.prototype.pick = function() {
      return this[Math.floor(Math.random()*this.length)];
    };
    
  4. pick(callback)
    Array.prototype.pick = function (callback)
      var element, index, length = this.length;
      for (index = 0; index < length; index++)
        element = this[index];
        if (Array.isArray(element) || (typeof element === 'object')) 
          element.pick(condition);
        if (callback(element,index))
    ...
    
  5. pickRandom()
    Array.prototype.pickRandom = function(){
       return this[Math.floor(Math.random() * this.length)];