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. 阿里云ECS hadoop+spark+zookeeper+hive code-server 集群搭建

    懒得重新排版然后发到博客了.用在线文档看吧 https://www.kdocs.cn/l/srV6o8rABW9V 用线上IDE(code-server)写scala的时候,出现BUG可以参考下面两篇 ...

  2. (Oracle)导出表结构

    DECLARE cursor t_name is SELECT rank() over(order by a.TABLE_NAME) as xiaolonglong,a.TABLE_NAME FROM ...

  3. .axios的特点有哪些

    从浏览器中创建XMLHttpRequests:node.js创建http请求:支持Promise API:拦截请求和响应:转换请求数据和响应数据:取消请求:自动换成json.axios中的发送字段的参 ...

  4. 题解 CF620E 【New Year Tree】

    有关dfs序的例题,需要有一定的位运算基础 题面 给定一个树,树上有颜色,将某一子树的颜色统一修改,求子树中颜色的数量 Solution 子树修改,子树求和,dfs序的知识(类似区间修改区间求和) 考 ...

  5. Java AQS的原理

    首先可以看:  http://ifeve.com/java-special-troops-aqs/ 再看  <Java并发编程的艺术>第5章 核心:同步器的acquire方法: 首先调用自 ...

  6. 应急响应-PDCERF模型 (转)

    目录 应急响应流程 防御模型 SDL 应急响应流程 很多人认为应急响应就是脸上被黑的机器去查查什么情况,是不是被中了botnet病毒.是不是被人中了rootkit等,是不是被挂了webshell等.应 ...

  7. Spring MVC—模型数据,转发重定向,静态资源处理方式

    Spring MVC处理模型数据 添加模型数据的方法 ModelAndView Map及Model SessionAttribute ModelAttribute Spring MVC转发和重定向 S ...

  8. Spring5源码,@Autowired

    一.@Autowired所具有的功能 二.在Spring中如何使用@Autowired 三.@Autowired注解背后的工作原理 一.@Autowired所具有的功能 @Autowired是一个用来 ...

  9. 31-1.解决service iptables save出错

    CentOS 7.x开始,CentOS开始使用systemd服务来代替daemon,原来管理系统启动和管理系统服务的相关命令全部由systemctl命令来代替.service命令只保留下了极少部分使用 ...

  10. 秒啊,速来get这9个jupyter实用技巧

    1 简介 jupyter notebook与jupyter lab作为广受欢迎的ide,尤其适合开展数据分析相关工作,而掌握它们相关的一些实用技巧,势必会大大提升日常工作效率.而今天我就来给大家介绍9 ...