js & array & shuffle

const list = [1, 2, 3, 4, 5, 6, 7, 8, 9];

list.sort(() => Math.random() - 0.5)
(9) [2, 6, 8, 1, 7, 5, 9, 3, 4] list.sort(() => Math.random() - 0.5)
(9) [8, 1, 3, 4, 9, 7, 6, 2, 5] list.sort(() => Math.random() - 0.5)
(9) [9, 8, 5, 7, 6, 1, 3, 2, 4] list.sort(() => Math.random() - 0.5)
(9) [1, 5, 7, 8, 6, 9, 2, 4, 3]

Array.sort()

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

shuffle 洗牌算法

https://zzk.cnblogs.com/my/s/blogpost-p?Keywords=shuffle

https://www.cnblogs.com/xgqfrms/p/11977189.html


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;
}

refs

https://flaviocopes.com/how-to-shuffle-array-javascript/


xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


js & array & shuffle的更多相关文章

  1. js Array数组的使用

    js Array数组的使用   Array是javascript中的一个事先定义好的对象(也可以称作一个类),可以直接使用 创建Array对象 var array=new Array(): 创建指定元 ...

  2. 从Chrome源码看JS Array的实现

    .aligncenter { clear: both; display: block; margin-left: auto; margin-right: auto } .crayon-line spa ...

  3. js Array 方法总结

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. js & array to string

    js & array to string https://stackoverflow.com/questions/13272406/convert-string-with-commas-to- ...

  5. From Ruby array to JS array in Rails- 'quote'?

    From Ruby array to JS array in Rails- 'quote'? <%= raw @location_list.as_json %>

  6. 解决js array的key不为数字时获取长度的问题

    最近写js时碰到了当数组key不为数字时,获取数组的长度为0 的情况. 1.问题场景 var arr = new Array(); arr[‘s1‘] = 1001; console.log(arr. ...

  7. JS Array.reverse 将数组元素颠倒顺序

    <pre><script type="text/javascript"> //JS Array.reverse 将数组元素颠倒顺序//在JavaScript ...

  8. js Array 中的 map, filter 和 reduce

    原文中部分源码来源于:JS Array.reduce 实现 Array.map 和 Array.filter Array 中的高阶函数 ---- map, filter, reduce map() - ...

  9. js array & unshift === push head

    js array & unshift === push head const arr = [1, 2, 3]; console.log(arr.unshift(4, 5)); // 5 con ...

随机推荐

  1. grpc-metadata

    https://github.com/grpc/grpc-go/blob/master/Documentation/grpc-metadata.md https://github.com/grpc/g ...

  2. oblet

      oblet - The Go Programming Language https://golang.google.cn/search?q=oblet // put enqueues a poin ...

  3. OPC UA 统一架构) (一)

    OPC UA 一 .OPC UA简介 OPC UA(OPC Unified Architecture)是下一代OPC统一体系架构,是一种基于服务的.跨越平台的解决方案. OPC UA具有如下特点: 1 ...

  4. socket更多方法

    一.socket的更多方法介绍 ###socket更多方法服务端套接字函数 s.bind() 绑定(主机,端口号)到套接字 s.listen() 开始TCP监听 s.accept() 被动接受TCP客 ...

  5. centos7 快速搭建redis集群环境

    本文主要是记录一下快速搭建redis集群环境的方式. 环境简介:centos 7  + redis-3.2.4 本次用两个服务6个节点来搭建:192.168.116.120  和  192.168.1 ...

  6. python爬虫selenium相关

    首先上很好用的selenium中文文档,基本上所有问题都能通过阅读此文档解决.可惜好像没找到翻译者名称. https://python-selenium-zh.readthedocs.io/zh_CN ...

  7. EXCEL序列

  8. dp practice 1

    https://codeforces.com/problemset/problem/553/A dp+组合数学 dp[i] 放前i种颜色的方法数 #include<bits/stdc++.h&g ...

  9. hdu3652B-number (数位dp)

    Problem Description A wqb-number, or B-number for short, is a non-negative integer whose decimal for ...

  10. linux 部署 .net core mvc

    1.本地编写一个mvc网站 代码编辑器:Visual studio 2017.2019.Visual Code 均可 1)搭建 略. (请自行搜索如何编辑mvc,或看文末参考链接) 2)配置 Prog ...