如何使用JavaScript Math.floor生成范围内的随机整数-已解决

news/2024/7/5 3:54:16

快速解决方案 (Quick Solution)

function randomRange(myMin, myMax) {return Math.floor(Math.random() * (myMax - myMin + 1) + myMin);
}

代码说明 (Code Explanation)

  • Math.random() generates our random number between 0 and ≈ 0.9.

    Math.random()生成介于0和≈0.9之间的随机数。

  • Before multiplying it, it resolves the part between parenthesis (myMax - myMin + 1) because of the grouping operator (   ).

    在将其相乘之前,由于分组运算符( ) ,它会解析括号之间的部分(myMax - myMin + 1) ( )

  • The result of that multiplication is followed by adding myMin and then "rounded" to the largest integer less than or equal to it (eg: 9.9 would result in 9)

    乘法的结果之后加上myMin ,然后将“四舍五入”为小于或等于它的最大整数(例如:9.9将得出9)

If the values were myMin = 1, myMax= 10, one result could be the following:

如果值为myMin = 1, myMax= 10 ,则结果可能如下:

  1. Math.random() = 0.8244326990411024

    Math.random() = 0.8244326990411024

  2. (myMax - myMin + 1) = 10 - 1 + 1 -> 10

    (myMax - myMin + 1) = 10 - 1 + 1 -> 10

  3. a * b =  8.244326990411024

    a * b = 8.244326990411024

  4. c + myMin = 9.244326990411024

    c + myMin = 9.244326990411024

  5. Math.floor(9.244326990411024) = 9

    Math.floor(9.244326990411024) = 9

randomRange should use both myMax and myMin, and return a random number in your range.

randomRange应该同时使用myMaxmyMin ,并返回您范围内的随机数。

You cannot pass the test if you are only re-using the function ourRandomRange inside your randomRange formula. You need to write your own formula that uses the variables myMax and myMin. It will do the same job as using ourRandomRange, but ensures that you have understood the principles of the Math.floor() and Math.random() functions.

如果仅在randomRange公式中重新使用函数ourRandomRange则无法通过测试。 您需要编写自己的使用变量myMaxmyMin 。 它将执行与使用ourRandomRange相同的工作,但确保您已了解Math.floor()Math.random()函数的原理。

翻译自: https://www.freecodecamp.org/news/generate-random-whole-numbers-within-a-range-javascript/


http://lihuaxi.xjx100.cn/news/237667.html

相关文章

九宫格代码

CGFloat margin 10.0f;CGFloat appViewWidth (375 - 4 * margin)/3;CGFloat appViewHeight (375 - 4 * margin)/3;//2. 完成布局设计//三列int totalloc 3;for (int i 0; i < 9; i) {int row i / totalloc;//行号int loc i % totalloc;//列号CGFloat appViewX margi…

区块链未来的发展前景是什么?

链客&#xff0c;专为开发者而生&#xff0c;有问必答&#xff01; 此文章来自链客区块链技术问答社区&#xff0c;未经允许拒绝转载。 区块链未来3到5年应该会出现职业井喷式开展&#xff0c;相应所需的人才必定水涨船高&#xff0c;每一个开发人员都不应该错失这样的时机。 …

python之CSV文件格式

1、csv文件是以一些以逗号分隔的值 import csv filename "wenjian.csv" with open(filename) as f:reader csv.reader()header next(reader)for index,column in enumerate(header):#enumerate函数获取每个元素的索引及其值print(index,column) 转载于:https://ww…

连信的protobuf数据格式

点击上方↑↑↑蓝字[协议分析与还原]关注我们“ 连信里用到的protobuf结构。”在看本文之前&#xff0c;可以先进行一下回顾&#xff0c;之前已经对协议的框架进行了整体的介绍&#xff1a;连信协议整体框架看了后结合自己的分析过程&#xff0c;应该有个初步的了解。后续会陆续…

Caused by: org.xml.sax.SAXParseException: 不允许有匹配 [xX][mM][lL] 的处理指令目标。

版权声明&#xff1a;本文为 testcs_dn(微wx笑) 原创文章&#xff0c;非商用自由转载-保持署名-注明出处&#xff0c;谢谢。 https://blog.csdn.net/testcs_dn/article/details/81001749 Caused by: org.xml.sax.SAXParseException: 不允许有匹配 "[xX][mM][lL]" 的处…

区块链的共识算法

链客&#xff0c;专为开发者而生&#xff0c;有问必答&#xff01; 此文章来自链客区块链技术问答社区&#xff0c;未经允许拒绝转载。 共同算法11 是经过特殊节点的投票&#xff0c;在短时间内完成对买卖的承认&#xff0c;假如利益不相干的若干个节点达到共同&#xff0c…

php的基础知识(四)

14、数组&#xff1a; 索引数组&#xff1a; 下标就是数字开始的。 $arr [a,b,c,1,2,3]; 关联数组&#xff1a; $arr [ a > b, c > d; e > f ]; 二维数组&#xff1a; 关联和索引混合的。 $arr [ a, b, c, d > [ e > h, f, g ], i, ]; 三维数组和多维数组。 …

如何使用Python和Tkinter构建Toy Markdown编辑器

Markdown editors are trending these days. Everybody is creating a markdown editor, and some of them are innovative while some of them are boring. Markdown编辑器近来呈趋势。 每个人都在创建降价编辑器&#xff0c;其中有些人很创新&#xff0c;而有些人很无聊。 A…