Nodejs Array Random random_item()

Here you can find the source of random_item()

Method Source Code

'use strict';//from   ww  w.ja va 2s . c  om

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

var printError = function (msg) {
    var newline = document.createElement('br');
    var content = document.createTextNode(msg);
    var messages = document.getElementById('messages');
    messages.appendChild(content);
    messages.appendChild(newline);
};

Related

  1. 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];
    ...
    
  2. randomPick()
    Array.prototype.randomPick = function () {
        return this[_.random(this.length - 1)];
    };
    
  3. randomPick()
    Array.prototype.randomPick = function () {
        return this[Math.floor(Math.random()*this.length)];
    };
    
  4. randomPick()
    Array.prototype.randomPick = function() {
      var index = Math.floor(Math.random() * this.length);
      return this[index];
    
  5. randomRemove()
    Array.prototype.randomRemove = function () {
      return this.splice(Math.floor(Math.random() * this.length), 1)[0];
    
  6. randomise()
    Array.prototype.randomise = function() {
        return this.sort(function() { return 0.5 - Math.random() });
    
  7. randomize()
    Array.prototype.randomize = function() {
      var result = [];
      while (this.length) {
        var index = this.indexOf(this.random());
        result.push(this.splice(index, 1)[0]);
      return result;
    
  8. randomize()
    Array.prototype.randomize = Array.prototype.randomize || function() {
      var result = [];
      var clone = this.slice();
      while (clone.length) {
        var index = clone.indexOf(clone.random());
        result.push(clone.splice(index, 1)[0]);
      return result;
    
  9. randomize()
    Array.prototype.randomize = function() {
        if (this.length < 2) {
            return this;
        var items = Array.from(this);
        var newArr = [];
        while (items.length > 0) {
            var entry = items.random();
            newArr.push(entry);
    ...