random array & shuffle 洗牌算法 / 随机算法
阅读原文时间:2023年07月08日阅读:5

random array & shuffle

shuffle 洗牌算法 / 随机算法

https://en.wikipedia.org/wiki/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;

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);



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]);



// 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

See the Pen Fisher–Yates shuffle by xgqfrms
(@xgqfrms) on CodePen.

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

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

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



xgqfrms 2012-2020

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


手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器