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
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 洗牌算法 / 随机算法的更多相关文章
- 【BZOJ-1965】SHUFFLE 洗牌 快速幂 + 拓展欧几里德
1965: [Ahoi2005]SHUFFLE 洗牌 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 541 Solved: 326[Submit][St ...
- 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 ...
- 1965: [Ahoi2005]SHUFFLE 洗牌
1965: [Ahoi2005]SHUFFLE 洗牌 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 408 Solved: 240[Submit][St ...
- 【bzoj1965】: [Ahoi2005]SHUFFLE 洗牌 数论-快速幂-扩展欧几里得
[bzoj1965]: [Ahoi2005]SHUFFLE 洗牌 观察发现第x张牌 当x<=n/2 x=2x 当x>n/2 x=2x-n-1 好像就是 x=2x mod (n+1) 就好 ...
- [AHOI2005] SHUFFLE 洗牌
1965: [Ahoi2005]SHUFFLE 洗牌 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 952 Solved: 630[Submit][St ...
- Fisher–Yates shuffle 洗牌算法(zz)
1,缘起 最近工作上遇到一个问题,即将一组数据,比如[A,B,C,D,E]其中的两个B,E按随机排列,其他的仍在原来的位置: 原始数组:[A,B,C,D,E] 随机字母:[B,D] 可能结果:[A,B ...
- Fisher–Yates shuffle 洗牌(shuffle)算法
今天在敲undersore的源码,数组里面有一个shuffle,把数组随机打乱. _.shuffle = function(obj) { var set = isArrayLike(obj) ? ob ...
- [LeetCode] Shuffle an Array 数组洗牌
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
- [LeetCode] 384. Shuffle an Array 数组洗牌
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
随机推荐
- IDEA_2019.1版本中Protobuf的使用
一.Protobuf是什么 Protobuf 是 Google 发布的开源项目,全称 Google Protocol(/'prəʊtəkɒl/,协议,草案) Buffers,是一种轻便高效的结构化数据 ...
- SQL函数知识点
SQL函数知识点 SQL题目(一) 1.查询部门编号为10的员工信息 select*from emp where empno=10; 2.查询年薪大于3万的人员的姓名与部门编号 select enam ...
- 数据水印 watermark
外发数据创建水印 产品通过对外发数据进行添加数据标记.自动生成水印.数据源追溯等功能,避免了内部人员外发数据泄露无法对事件追溯,提高了数据传递的安全性和可追溯能力. 数据水印系统_数据安全管理工具_[ ...
- AWS Lightsail 开启 Root 登陆权限
将下面代码中的第一句中的 Passwd 改为自己将要设置的密码,否则默认 root 密码为 Passwd. #!/bin/bash echo root:Passwd |sudo chpasswd ro ...
- 浅析 record 使用场景
浅析 record 使用场景 Intro 之前我们有介绍过 record 基本知识,record 会实现基于值的类型比较,最近遇到的几个问题觉得用 record 来解决会非常方便,分享一下 基于值的类 ...
- .NET5 它来了!微软大一统时代来临!
今天双11,Microsoft released.NET 5(在他们的开发博客上同时发布).新版本的重点是改进.NET Core 3.1: 更小的单文件应用程序.对 Windows ARM64的支持以 ...
- python join()方法的使用,可以应用到tcp压测发送指定数据长度的应用
Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串.其中,序列中的元素应是字符串类型. 学习join()方法主要是为了配合随机数的使用,生产某个指定位数的随机数,在t ...
- P4587 [FJOI2016]神秘数(主席树)
题意:给出1e5个数 查询l,r区间内第一个不能被表示的数 比如1,2,4可以用子集的和表示出[1,7] 所以第一个不能被表示的是8 题解:先考虑暴力的做法 把这个区间内的数字按从小到大排序后 从前往 ...
- 2020牛客暑期多校训练营(第八场)Game SET
传送门:Game SET 题意 一套牌有四种属性,每种属性都有三种特征,shapes (one, two, or three), shape (diamond, squiggle, or oval), ...
- Codeforces Round #655 (Div. 2) B. Omkar and Last Class of Math
题目链接:https://codeforces.com/contest/1372/problem/B 题意 给出一个正整数 $n$,找到两个正整数 $a,b$ 满足 $a+b = n$ 且 $LCM( ...