From d9e9adb5f80a819fe46349bcf6d1faec734b09cd Mon Sep 17 00:00:00 2001 From: Irvin Date: Wed, 16 Feb 2022 02:07:31 +0800 Subject: remove span tag in zh-CN --- .../reference/global_objects/math/random/index.html | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'files/zh-cn/web/javascript/reference/global_objects/math/random') diff --git a/files/zh-cn/web/javascript/reference/global_objects/math/random/index.html b/files/zh-cn/web/javascript/reference/global_objects/math/random/index.html index efc11b6894..e25608ec05 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/math/random/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/math/random/index.html @@ -50,11 +50,11 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Math/random

这个例子返回了一个在指定值之间的随机整数。这个值不小于 min (如果 min 不是整数,则不小于 min 的向上取整数),且小于(不等于)max

-
function getRandomInt(min, max) {
-  min = Math.ceil(min);
-  max = Math.floor(max);
-  return Math.floor(Math.random() * (max - min)) + min; //不含最大值,含最小值
-}
+
function getRandomInt(min, max) {
+  min = Math.ceil(min);
+  max = Math.floor(max);
+  return Math.floor(Math.random() * (max - min)) + min; //不含最大值,含最小值
+}

也许很容易想到用 Math.round() 来实现,但是这会导致你的随机数处于一个不均匀的分布,这可能不符合你的需求。

@@ -64,11 +64,11 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Math/random

上一个例子提到的函数 getRandomInt() 结果范围包含了最小值,但不含最大值。如果你的随机结果需要同时包含最小值和最大值,怎么办呢?  getRandomIntInclusive() 函数可以实现。

-
function getRandomIntInclusive(min, max) {
-  min = Math.ceil(min);
-  max = Math.floor(max);
-  return Math.floor(Math.random() * (max - min + 1)) + min; //含最大值,含最小值 
-}
+
function getRandomIntInclusive(min, max) {
+  min = Math.ceil(min);
+  max = Math.floor(max);
+  return Math.floor(Math.random() * (max - min + 1)) + min; //含最大值,含最小值 
+}

规范

-- cgit v1.2.3-54-g00ecf