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

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

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 发布文章使用:只允许注册用户才可以访问!


random array & shuffle 洗牌算法 / 随机算法的更多相关文章

  1. 【BZOJ-1965】SHUFFLE 洗牌 快速幂 + 拓展欧几里德

    1965: [Ahoi2005]SHUFFLE 洗牌 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 541  Solved: 326[Submit][St ...

  2. BZOJ 1965: [Ahoi2005]SHUFFLE 洗牌( 数论 )

    对于第x个数, 下一轮它会到位置p. 当x<=N/2, p = x*2 当x>N/2, p = x*2%(N+1) 所以p = x*2%(N+1) 设一开始的位置为t, 那么t*2M%(N ...

  3. 1965: [Ahoi2005]SHUFFLE 洗牌

    1965: [Ahoi2005]SHUFFLE 洗牌 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 408  Solved: 240[Submit][St ...

  4. 【bzoj1965】: [Ahoi2005]SHUFFLE 洗牌 数论-快速幂-扩展欧几里得

    [bzoj1965]: [Ahoi2005]SHUFFLE 洗牌 观察发现第x张牌 当x<=n/2 x=2x 当x>n/2 x=2x-n-1 好像就是 x=2x mod (n+1)  就好 ...

  5. [AHOI2005] SHUFFLE 洗牌

    1965: [Ahoi2005]SHUFFLE 洗牌 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 952  Solved: 630[Submit][St ...

  6. Fisher–Yates shuffle 洗牌算法(zz)

    1,缘起 最近工作上遇到一个问题,即将一组数据,比如[A,B,C,D,E]其中的两个B,E按随机排列,其他的仍在原来的位置: 原始数组:[A,B,C,D,E] 随机字母:[B,D] 可能结果:[A,B ...

  7. Fisher–Yates shuffle 洗牌(shuffle)算法

    今天在敲undersore的源码,数组里面有一个shuffle,把数组随机打乱. _.shuffle = function(obj) { var set = isArrayLike(obj) ? ob ...

  8. [LeetCode] Shuffle an Array 数组洗牌

    Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...

  9. [LeetCode] 384. Shuffle an Array 数组洗牌

    Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...

随机推荐

  1. Connection Pool

    MySQL :: MySQL Connector/NET Developer Guide :: 4.3 Managing a Connection Pool in Connector/NET http ...

  2. functools.singledispatchmethod(Python 3.8) | 码农网 https://www.codercto.com/a/83245.html

    functools.singledispatchmethod(Python 3.8) | 码农网 https://www.codercto.com/a/83245.html

  3. High Performance Networking in Google Chrome 进程间通讯(IPC) 多进程资源加载

    小结: 1. 小文件存储于一个文件中: 在内部,磁盘缓存(disk cache)实现了它自己的一组数据结构, 它们被存储在一个单独的缓存目录里.其中有索引文件(在浏览器启动时加载到内存中),数据文件( ...

  4. http发送

    package cn.com.yitong.wdph.util; import java.io.BufferedReader;import java.io.InputStream;import jav ...

  5. java小技巧

    String 转 Date String classCode = RequestHandler.getString(request, "classCode"); SimpleDat ...

  6. 2.DHCP的基本概念

    1.DHCP典型组网 DHCP组网中,包括以下三种角色: DHCP服务器 DHCP服务器负责从地址池中选择IP地址分配至DHCP客户端,还可以为DHCP客户端提供其他网络参数,如默认网关地址.DNS服 ...

  7. 关于Java注解(annotation)的简单理解

    一.什么是注解? 从 JDK5 开始,Java增加对元数据的支持,也就是注解.简单理解就是代码里的特殊标志,这些标志可以在编译,类加载,运行时被读取,并执行相应的处理,以便于其他工具补充信息或者进行部 ...

  8. P1908 逆序对——树状数组&离散化&快读快写の学习

    题目简述: 对于给定的一段正整数序列,逆序对就是序列中 a_i>a_jai​>aj​ 且 i<ji<j 的有序对. 输出序列中逆序对的数目. 知识补充: 树状数组: 这东西就是 ...

  9. 二进制方法-部署k8s集群部署1.18版本

    二进制方法-部署k8s集群部署1.18版本 1. 前置知识点 1.1 生产环境可部署kubernetes集群的两种方式 目前生产部署Kubernetes集群主要有两种方式 kuberadm Kubea ...

  10. 2288.【POJ Challenge】生日礼物 链表+堆+贪心

    BZOJ2288 [POJ Challenge]生日礼物 题意: 给一个长度为\(n\)的数组,最多可以选\(m\)个连续段,问选取的最大值是多少 题解: 先把连续的符号相同的值合并,头和尾的负数去掉 ...