random array & shuffle

shuffle 洗牌算法 / 随机算法

https://en.wikipedia.org/wiki/Fisher–Yates_shuffle

ES6 version


  1. "use strict";
  2. /**
  3. *
  4. * @author xgqfrms
  5. * @license MIT
  6. * @copyright xgqfrms
  7. * @created 2020-07-20
  8. * @modified
  9. *
  10. * @description shuffle 洗牌算法
  11. * @difficulty Easy
  12. * @complexity O(n)
  13. * @augments
  14. * @example
  15. * @link https://www.cnblogs.com/xgqfrms/p/11977189.html
  16. * @solutions
  17. *
  18. */
  19. const log = console.log;
  20. const shuffle = (arr = []) => {
  21. let len = arr.length;
  22. while (len > 1){
  23. // Math.floor
  24. const index = Math.floor(Math.random() * len--);
  25. // ES6 swap
  26. [
  27. arr[len],
  28. arr[index],
  29. ] = [
  30. arr[index],
  31. arr[len],
  32. ];
  33. }
  34. return arr;
  35. }
  36. const noForArrayAutoGenerator = (len = 100, type = `number`) => {
  37. if (type === `number`) {
  38. // number array
  39. return [...``.padStart(len, ` `)].map((item, i) => i + 1);
  40. } else if(`string`) {
  41. // string array
  42. return [...``.padStart(len, ` `)].map((item, i) => i + 1 + ``);
  43. } else {
  44. // mixed array
  45. return [...``.padStart(len, ` `)].map((item, i) => i + 1).map((item, i) => i % 2 === 0 ? item : item + ``);
  46. }
  47. }
  48. const arr = noForArrayAutoGenerator(10);
  49. const test = shuffle(arr);
  50. log(`test`, test);

ES5 version

  1. function shuffle(arr) {
  2. let m = arr.length;
  3. while (m > 1){
  4. let index = Math.floor(Math.random() * m--);
  5. [arr[m] , arr[index]] = [arr[index] , arr[m]]
  6. }
  7. return arr;
  8. }
  9. // test
  10. shuffle([1,2,3,4,5,6,7,8]);

prototype version

  1. // Fisher–Yates shuffle
  2. "use strict";
  3. /**
  4. *
  5. * @author xgqfrms
  6. * @license MIT
  7. * @copyright xgqfrms
  8. * @created 2020-07-20
  9. * @modified
  10. *
  11. * @description shuffle 洗牌算法
  12. * @difficulty Easy
  13. * @complexity O(n)
  14. * @augments
  15. * @example
  16. * @link https://www.cnblogs.com/xgqfrms/p/11977189.html
  17. * @solutions
  18. *
  19. */
  20. const log = console.log;
  21. // prototype
  22. Array.prototype.shuffle = function() {
  23. var arr = this;
  24. log(`arr === this`, arr)
  25. var len = arr.length;
  26. for (let i = 0; i < len; i++) {
  27. // randomIndex
  28. var index = Math.floor(Math.random() * (i + 1));
  29. // swap
  30. var temp = arr[index];
  31. arr[index] = arr[i];
  32. arr[i] = temp;
  33. }
  34. return arr;
  35. };
  36. const noForArrayAutoGenerator = (len = 100, type = `number`) => {
  37. if (type === `number`) {
  38. // number array
  39. return [...``.padStart(len, ` `)].map((item, i) => i + 1);
  40. } else if(`string`) {
  41. // string array
  42. return [...``.padStart(len, ` `)].map((item, i) => i + 1 + ``);
  43. } else {
  44. // mixed array
  45. return [...``.padStart(len, ` `)].map((item, i) => i + 1).map((item, i) => i % 2 === 0 ? item : item + ``);
  46. }
  47. }
  48. const arr = noForArrayAutoGenerator(10);
  49. // test
  50. const test = arr.shuffle();
  51. 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. 广告召回 Query-Ad Matching

    小结: 1.最为基础的召回链路就是要保证召回层的相关性,但是相关性高的广告并不一定具有很高的商业价值,所以开始尝试将一些商业化业务指标作为召回的依据 百度凤巢新一代广告召回系统--"莫比乌斯 ...

  2. UNIX DOMAIN SOCKETS IN GO unix域套接字

    Unix domain sockets in Go - Golang News https://golangnews.org/2019/02/unix-domain-sockets-in-go/ pa ...

  3. MySQL 高性能优化规范建议

    数据库命令规范 所有数据库对象名称必须使用小写字母并用下划线分割 所有数据库对象名称禁止使用 MySQL 保留关键字(如果表名中包含关键字查询时,需要将其用单引号括起来) 数据库对象的命名要能做到见名 ...

  4. ASP.NET Core 5.0 MVC中的视图分类——布局视图、启动视图、具体视图、分部视图

    一.创建MVC应用程序 创建后的项目 二.(全局性)启动视图 _ViewStart.cshtml 顾名思义,就是在View开始执行之前执行,而且是每一个View, 它的预设内容是 @{ Layout ...

  5. Docker系列(一)Docker概述,核心概念讲解,安装部署

    部分内容参考链接: Docker实战总结(非常全面,建议收藏) 一. Docker概述 Docker是一个开源的应用容器引擎(基于Go语言开发),让开发者可以打包他们的应用以及依赖包到一个可移植的容器 ...

  6. docker镜像加速,docker更换为国内镜像

    docker镜像加速,docker更换为国内镜像 一.使用官方镜像 二.Docker守护进程配置加速器 相关博文原文地址: CSDN:让我思考一下 :docker更换为国内镜像 一.使用官方镜像 Do ...

  7. Jcrop图片裁剪

    一.引入js和css 二.实现 1.jsp页面 <%-- Created by IntelliJ IDEA. User: a Date: 2019/8/19 Time: 9:36 To chan ...

  8. Codeforces Round #678 (Div. 2)【ABCD】

    比赛链接:https://codeforces.com/contest/1436 A. Reorder 题解 模拟一下这个二重循环发现每个位置数最终都只加了一次. 代码 #include <bi ...

  9. 2020牛客暑期多校训练营(第八场)Interesting Computer Game

    传送门:Interesting Computer Game 题意 给出n对数,你可以操作n次,每次操作只能在下面三种中选择一种,问最多可以选多少个不同的数字. 什么都不做 如果a[i]以前没选过,那么 ...

  10. 【noi 2.6_7624】山区建小学(DP)

    题意:在m个村庄建n个小学,求所有村到最近小学的距离总的最小值. 解法:由于题目是求"离最近的学校",而不是前一个学校,所以枚举学校的具体位置不方便,可转化成区间(学校居区间中间) ...