zoukankan      html  css  js  c++  java
  • xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

    random array & shuffle

    shuffle 洗牌算法 / 随机算法

    https://en.wikipedia.org/wiki/Fisher–Yates_shuffle

    ES6 version

    
    "use strict";
    
    /**
     *
     * @author xgqfrms
     * @license MIT
     * @copyright xgqfrms
     * @created 2020-07-20
     * @modified
     *
     * @description shuffle 洗牌算法
     * @difficulty Easy
     * @complexity O(n)
     * @augments
     * @example
     * @link https://www.cnblogs.com/xgqfrms/p/11977189.html
     * @solutions
     *
     */
    
    const log = console.log;
    
    const shuffle = (arr = []) => {
      let len = arr.length;
      while (len > 1){
        // Math.floor
        const index = Math.floor(Math.random() * len--);
        // ES6 swap
        [
          arr[len],
          arr[index],
        ] = [
          arr[index],
          arr[len],
        ];
      }
      return arr;
    }
    
    const noForArrayAutoGenerator = (len = 100, type = `number`) => {
      if (type === `number`) {
        // number array
        return [...``.padStart(len, ` `)].map((item, i) => i + 1);
      } else if(`string`) {
        // string array
        return [...``.padStart(len, ` `)].map((item, i) => i + 1 + ``);
      } else {
        // mixed array
        return [...``.padStart(len, ` `)].map((item, i) => i + 1).map((item, i) => i % 2 === 0 ? item : item + ``);
      }
    }
    
    const arr = noForArrayAutoGenerator(10);
    
    const test = shuffle(arr);
    
    log(`test`, test);
    
    
    

    ES5 version

    function shuffle(arr) {
        let m = arr.length;
        while (m > 1){
            let index = Math.floor(Math.random() * m--);
            [arr[m] , arr[index]] = [arr[index] , arr[m]]
        }
        return arr;
    }
    
    // test
    shuffle([1,2,3,4,5,6,7,8]);
    
    

    prototype version

    // Fisher–Yates shuffle
    
    
    "use strict";
    
    /**
     *
     * @author xgqfrms
     * @license MIT
     * @copyright xgqfrms
     * @created 2020-07-20
     * @modified
     *
     * @description shuffle 洗牌算法
     * @difficulty Easy
     * @complexity O(n)
     * @augments
     * @example
     * @link https://www.cnblogs.com/xgqfrms/p/11977189.html
     * @solutions
     *
     */
    
    const log = console.log;
    
    // prototype
    Array.prototype.shuffle = function() {
      var arr = this;
      log(`arr === this`, arr)
      var len = arr.length;
      for (let i = 0; i < len; i++) {
        // randomIndex
        var index = Math.floor(Math.random() * (i + 1));
        // swap
        var temp = arr[index];
        arr[index] = arr[i];
        arr[i] =  temp;
      }
      return arr;
    };
    
    
    const noForArrayAutoGenerator = (len = 100, type = `number`) => {
      if (type === `number`) {
        // number array
        return [...``.padStart(len, ` `)].map((item, i) => i + 1);
      } else if(`string`) {
        // string array
        return [...``.padStart(len, ` `)].map((item, i) => i + 1 + ``);
      } else {
        // mixed array
        return [...``.padStart(len, ` `)].map((item, i) => i + 1).map((item, i) => i % 2 === 0 ? item : item + ``);
      }
    }
    
    const arr = noForArrayAutoGenerator(10);
    
    // test
    const test = arr.shuffle();
    
    log(`test`, test);
    
    
    

    demo

    refs

    https://codepen.io/xgqfrms/pen/Bayabba

    https://gaohaoyang.github.io/2016/10/16/shuffle-algorithm/#top

    https://juejin.im/post/5d004ad95188257c6b518056


    Flag Counter

    ©xgqfrms 2012-2020

    www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


  • 相关阅读:
    【TouchGFX】使用CubeMX创建touchgfx项目 -- 初始篇
    opencv haarcascades 下载
    更换 Anaconda 源(贼快)
    yii2 允许跨域
    sublime text 3 安装 pyv8 失败的解决办法
    win10 anaconda cuda11.1 安装 tensorlfow-gpu 环境
    nginx 403转404
    python requests 全部异常
    win10 anaconda 安装 tensorflow-gpu 及 jupyter notebook
    【PHP】自有图片服务器,图片动态裁剪缩放
  • 原文地址:https://www.cnblogs.com/xgqfrms/p/11977189.html
Copyright © 2011-2022 走看看