js随机码之乱序数组
2024-10-15 22:34:24
<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head> <body> </body>
<script>
var times = 100000;
var res = {}; for (var i = 0; i < times; i++) {
var arr = shuffle([1, 2, 3]); var key = JSON.stringify(arr);
res[key] ? res[key]++ : res[key] = 1;
} // 为了方便展示,转换成百分比
for (var key in res) {
res[key] = res[key] / times * 100 + '%'
} //ES5
function shuffle(a) {
var j, x, i;
for (i = a.length; i; i--) {
j = Math.floor(Math.random() * i);
x = a[i - 1];
a[i - 1] = a[j];
a[j] = x;
}
return a;
} //ES6
function shuffle(a) {
for (let i = a.length; i; i--) {
let j = Math.floor(Math.random() * i);
[a[i - 1], a[j]] = [a[j], a[i - 1]];
}
return a;
} console.log(res)
</script> </html>
最新文章
- HttpWebRequest的GetResponse或GetRequestStream偶尔超时 + 总结各种超时死掉的可能和相应的解决办法
- linux系统下的权限知识梳理
- SPSS数据分析—广义估计方程
- LeetCode 2. Add Two Numbers swift
- VS2015+Windows 10下配置opencv3.0
- js获取服务器控件DropDownList所选中的各项属性
- CSS实现圆角,三角,五角星,五边形,爱心,12角星,8角星,圆,椭圆,圆圈,八卦
- WEB和APP谁是互联网未来
- ajax bookstrap美化网页,并实现页面的加载,删除与查看详情
- Asp.Net Core 2.1+的视图缓存(响应缓存)
- 【IOS 开发】Objective - C 面向对象高级特性 - 包装类 | 类处理 | 类别 | 扩展 | 协议 | 委托 | 异常处理 | 反射
- Jeecg
- 兼容ie,火狐的判断回车键js脚本
- BD是什么角色
- Java通过POI读取Excel
- [Windows Azure] Virtual Machine and Cloud Service Sizes for Windows Azure
- 23种设计模式之命令模式(Command)
- js跨域请求提示函数未定义的问题
- ubuntu 可能的依赖包,安装过程中根据需要安装
- java作业调度框架Quartz
热门文章
- BZOJ 1208: [HNOI2004]宠物收养所 SET的妙用
- jquery 图片预加载
- JSON在PHP中的基本应用
- C++使用autoreconf -vi出现error: possibly undefined macro: AC_LIBTOOL_WIN32_DLL If this token and others are legitimate, please use m4_pattern_allow. See the Autoconf documentation.
- cocos2d0基础篇笔记一
- IIS 未能从程序集“System.ServiceModel, Version=3.0.0.0, Culture=neutral,
- svn提交代码忘写注释怎么办,我想补充上去?
- python测试开发django-6.模板中include使用
- Dwz手册的补充说明和常见问题
- 郑捷2017年电子工业出版社出版的图书《NLP汉语自然语言处理原理与实践》