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 ...
随机推荐
- 广告召回 Query-Ad Matching
小结: 1.最为基础的召回链路就是要保证召回层的相关性,但是相关性高的广告并不一定具有很高的商业价值,所以开始尝试将一些商业化业务指标作为召回的依据 百度凤巢新一代广告召回系统--"莫比乌斯 ...
- UNIX DOMAIN SOCKETS IN GO unix域套接字
Unix domain sockets in Go - Golang News https://golangnews.org/2019/02/unix-domain-sockets-in-go/ pa ...
- MySQL 高性能优化规范建议
数据库命令规范 所有数据库对象名称必须使用小写字母并用下划线分割 所有数据库对象名称禁止使用 MySQL 保留关键字(如果表名中包含关键字查询时,需要将其用单引号括起来) 数据库对象的命名要能做到见名 ...
- ASP.NET Core 5.0 MVC中的视图分类——布局视图、启动视图、具体视图、分部视图
一.创建MVC应用程序 创建后的项目 二.(全局性)启动视图 _ViewStart.cshtml 顾名思义,就是在View开始执行之前执行,而且是每一个View, 它的预设内容是 @{ Layout ...
- Docker系列(一)Docker概述,核心概念讲解,安装部署
部分内容参考链接: Docker实战总结(非常全面,建议收藏) 一. Docker概述 Docker是一个开源的应用容器引擎(基于Go语言开发),让开发者可以打包他们的应用以及依赖包到一个可移植的容器 ...
- docker镜像加速,docker更换为国内镜像
docker镜像加速,docker更换为国内镜像 一.使用官方镜像 二.Docker守护进程配置加速器 相关博文原文地址: CSDN:让我思考一下 :docker更换为国内镜像 一.使用官方镜像 Do ...
- Jcrop图片裁剪
一.引入js和css 二.实现 1.jsp页面 <%-- Created by IntelliJ IDEA. User: a Date: 2019/8/19 Time: 9:36 To chan ...
- Codeforces Round #678 (Div. 2)【ABCD】
比赛链接:https://codeforces.com/contest/1436 A. Reorder 题解 模拟一下这个二重循环发现每个位置数最终都只加了一次. 代码 #include <bi ...
- 2020牛客暑期多校训练营(第八场)Interesting Computer Game
传送门:Interesting Computer Game 题意 给出n对数,你可以操作n次,每次操作只能在下面三种中选择一种,问最多可以选多少个不同的数字. 什么都不做 如果a[i]以前没选过,那么 ...
- 【noi 2.6_7624】山区建小学(DP)
题意:在m个村庄建n个小学,求所有村到最近小学的距离总的最小值. 解法:由于题目是求"离最近的学校",而不是前一个学校,所以枚举学校的具体位置不方便,可转化成区间(学校居区间中间) ...