当前位置:网站首页>random number from min to max
random number from min to max
2022-08-06 21:41:00 【blue sky】
一、从 min 到 max 的随机数
内建函数 Math.random() 会创建一个在 0 到 1 之间(不包括 1)的随机数.
编写一个 random(min, max) 函数,to generate an in min 到 max 之间的随机浮点数(不包括 max)).
We need to set the interval 0…1 中的所有值“映射”为范围在 min 到 max 中的值.
This can be done in two stages:
- 如果我们将
0…1的随机数乘以 max-min,Then the range of random numbers will be from0…1增加到0..max-min. - 现在,If we combine random numbers with min 相加,Then the range of random numbers will be min 到 max.
function random(min, max) {
return min + Math.random() * (max - min);
}
二、从 min 到 max 的随机整数
创建一个函数 randomInteger(min, max),The function generates a range in min 到 max 中的随机整数,包括 min 和 max.
在 min…max All numbers in the range must occur with the same probability.
Simple but wrong solution
The simplest but wrong solution is to generate a range in min 到 max 的值,and take the value after rounding it:
function randomInteger(min, max) {
let rand = min + Math.random() * (max - min);
return Math.round(rand);
}
This function can work,但不正确.Get edge values min 和 max is twice as likely as the other values.
If you run the above example multiple times,You will see easily 2 出现的频率最高.
发生这种情况是因为 Math.round() 从范围 1…3 中获得随机数,And round up as shown below:
values from 1 ... to 1.4999999999 become 1
values from 1.5 ... to 2.4999999999 become 2
values from 2.5 ... to 2.9999999999 become 3
现在我们可以清楚地看到 1 的值比 2 twice less.和 3 一样.
正确的解决方案
There are many correct solutions to this problem.One of them is to adjust the boundaries of the range of values.To ensure the same value range,We can generate from 0.5 到 3.5 的值,This adds the desired probability to the boundaries of the range of values:
function randomInteger(min, max) {
// 现在范围是从 (min-0.5) 到 (max+0.5)
let rand = min - 0.5 + Math.random() * (max - min + 1);
return Math.round(rand);
}
另一种方法是使用 Math.floor to take the range from min 到 max+1 的随机数:
function randomInteger(min, max) {
// here rand is from min to (max+1)
let rand = min + Math.random() * (max + 1 - min);
return Math.floor(rand);
}
All intervals are now mapped this way:
values from 1 ... to 1.9999999999 become 1
values from 2 ... to 2.9999999999 become 2
values from 3 ... to 3.9999999999 become 3
All intervals are the same length,So that the final can be evenly distributed.
边栏推荐
猜你喜欢

伪装马上线Cobalstrike

8086CPU标志寄存器

硅谷课堂第十一课-公众号消息和微信授权

积极防御体系进阶:《DevSecOps敏捷安全》

嵌入式分享合集30

Cascade WPF 】 【 Combobox and its linkage with ListView

package DotaChessSelfPlay is not in GOROOT以及 relative import paths are not supported in module mode

【无标题】camera2的相关介绍

收获首届算力大会“创新先锋”,济南超算“数据存储集群系统”揭示了什么趋势

一文带你了解webrtc基本原理(动手实现1v1视频通话)
随机推荐
【无标题】
Redis 复习计划 - String内存开销问题以及基本/扩展数据类型的使用
DataFrame一行拆成多行
利用类加载器获取文件路径报错空指针异常
leetcode 769. Max Chunks To Make Sorted 最多能完成排序的块(中等)
2. 顺序表和链表的比较
Four cases of adjustment after the AVL tree is inserted into a new node (left single rotation, right single rotation, double rotation)
8086CPU标志寄存器
Open legendary GM website
Servlet中上传文件(用到DiskFileItemFactory)
Before start of result set报错(已解决)
xp系统怎么升级win7系统版本
硅谷课堂第十课-营销模块和公众号菜单管理
that if the size of dimension 1 of the input is not 1, the ONNX model will return an error
微信小程序发布动态页面模板
package DotaChessSelfPlay is not in GOROOT以及 relative import paths are not supported in module mode
0x000000f4蓝屏是怎么回事 win7蓝屏0x000000f4解决方法
硅谷课堂第五课-腾讯云对象存储和课程分类管理
五、练习:高精度
win32 overview and framework