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 ...
随机推荐
- SpringBoot 报错: Circular view path [readingList] 解决办法
spring boot报错: Circular view path [readingList]: would dispatch back to the current handler URL [/re ...
- 控制反转 依赖注入 main函数
通过依赖注入.服务定位实现控制反转 Go kit - Frequently asked questions https://gokit.io/faq/ Dependency Injection - W ...
- Hive 使用总结
1 带分区列的表更改列类型 常见的一个场景是Hive里面一个带分区的表,原来是int类型的字段,后来发现数据超过了int的最大值,要改成bigint.或者是bigint要改string或decimal ...
- mysql int类型 int(11) 和int(2)区别
CREATE TABLE `learn` ( `id` int(11) unsigned NOT NULL, `exp` int(2) DEFAULT 0, PRIMARY KEY (`id`)) E ...
- php 7.4 vcruntime140.dll not compatible with PHP
安装PHP7.4以上版本时,在运行PHP时会提示如下: PHP Warning: 'vcruntime140.dll' 14.0 is not compatible with this PHP bui ...
- 解决GraphViz's executables not found
用python做决策树可视化时,出现了下面的错误: 于是安装Graphviz,并将其添加到path的环境变量. Graphviz下载 提取码:fmst 但是已经安装了pydotplus且import之 ...
- spark sql优化
1.内存优化 1.1.RDD RDD默认cache仅使用内存 可以看到使用默认cache时,四个分区只在内存中缓存了3个分区,4.4G的数据 使用kryo序列化+MEMORY_ONLY_SER 可以看 ...
- H - 看病要排队
看病要排队这个是地球人都知道的常识.不过经过细心的0068的观察,他发现了医院里排队还是有讲究的.0068所去的医院有三个医生(汗,这么少)同时看病.而看病的人病情有轻重,所以不能根据简单的先来先服务 ...
- AYIT-2020 609暑假集训第一周周赛题 A - A计划
可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验.魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老.年迈的国王正是心急如焚,告招天下 ...
- P2801 教主的魔法 (分块)
题目传送 长度为\(n(n\le 1000000)\)的数组,\(q(q\le 3000)\) 次操作.修改操作即将某个区间的值增加某个不大于1000的值,查询操作即查询某个区间比C大于等于的数有多少 ...