xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

random array & shuffle 洗牌算法 / 随机算法

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



©xgqfrms 2012-2020

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


posted @ 2019-12-03 15:47  xgqfrms  阅读(685)  评论(7编辑  收藏  举报