如何随机生成-​​0.5和0.5范围内的数字?

时间:2022-11-25 20:15:28

In javascript how do I randomly generate a number between -0.5 and 0.5? The following only seems to give positive numbers, and no negative numbers.

在javascript中如何随机生成介于-0.5和0.5之间的数字?以下似乎只给出正数,没有负数。

Math.random(-0.15, 0.15)

2 个解决方案

#1


Quoting MDN on Math.random,

在Math.random上引用MDN,

The Math.random() function returns a floating-point, pseudo-random number in the range [0, 1) that is, from 0 (inclusive) up to but not including 1 (exclusive)

Math.random()函数返回[0,1]范围内的浮点伪随机数,从0(包括)到最大但不包括1(不包括)

So, generate random numbers between 0 and 1 and subtract 0.5 from it.

因此,生成0到1之间的随机数,并从中减去0.5。

Math.random() - 0.5

Note: Math.random() doesn't expect any parameters. So, you cannot specify any range.

注意:Math.random()不期望任何参数。因此,您无法指定任何范围。


If you want to generate random numbers between a range, use the example snippets in MDN,

如果要在范围之间生成随机数,请使用MDN中的示例代码段,

// Returns a random number between min (inclusive) and max (exclusive)
function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min;
}

Demo with Range

带范围的演示

function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min;
}


function appendToResult(value) {
  document.getElementById("result").innerHTML += value + "<br />";
}

document.getElementById("result").innerHTML = "";

for (var i = 0; i < 10; i += 1) {
  appendToResult(getRandomArbitrary(-0.35, 0.35));
}
<pre id="result" />

This works because, the expression

这是因为,表达式

Math.random() * (max - min) + min

will be evaluated in the following manner. First,

将按以下方式评估。第一,

Math.random()

will be evaluated to get a random number in the range 0 and 1 and then

将被评估以获得0和1范围内的随机数,然后

Math.random() * (max - min)

it will be multiplied with the difference between max and min value. In your case, lets say max is 0.35 and min is -0.35. So, max - min becomes 0.7. So, Math.random() * (max - min) gives a number in the range 0 and 0.7 (because even if Math.random() produces 1, when you multiply it with 0.7, the result will become 0.7). Then, you do

它将乘以最大值和最小值之间的差值。在你的情况下,假设max为0.35,min为-0.35。所以,max - min变为0.7。因此,Math.random()*(max - min)给出一个0和0.7范围内的数字(因为即使Math.random()产生1,当你将它乘以0.7时,结果将变为0.7)。然后,你这样做

Math.random() * (max - min) + min

So, for your case, that basically becomes,

所以,对于你的情况,这基本上变成了,

Math.random() * (0.35 - (-0.35)) + (-0.35)
(Math.random() * 0.7) - 0.35

So, if (Math.random() * 0.7) generates any value greater than 0.35, since you subtract 0.35 from it, it becomes some number between 0 and 0.35. If (Math.random() * 0.7) generates a value lesser than 0.35, since you subtract 0.35 from it, it becomes a number between -0.35 and 0.

因此,如果(Math.random()* 0.7)生成任何大于0.35的值,因为从中减去0.35,它将变为介于0和0.35之间的某个数字。如果(Math.random()* 0.7)生成的值小于0.35,因为从中减去0.35,它将变为介于-0.35和0之间的数字。

#2


You can use Math.random() to get a number between 0 and 1 then reduce .5 to set the lower and upper bounds

您可以使用Math.random()来获取0到1之间的数字,然后减少.5来设置下限和上限

Math.random() - .5

#1


Quoting MDN on Math.random,

在Math.random上引用MDN,

The Math.random() function returns a floating-point, pseudo-random number in the range [0, 1) that is, from 0 (inclusive) up to but not including 1 (exclusive)

Math.random()函数返回[0,1]范围内的浮点伪随机数,从0(包括)到最大但不包括1(不包括)

So, generate random numbers between 0 and 1 and subtract 0.5 from it.

因此,生成0到1之间的随机数,并从中减去0.5。

Math.random() - 0.5

Note: Math.random() doesn't expect any parameters. So, you cannot specify any range.

注意:Math.random()不期望任何参数。因此,您无法指定任何范围。


If you want to generate random numbers between a range, use the example snippets in MDN,

如果要在范围之间生成随机数,请使用MDN中的示例代码段,

// Returns a random number between min (inclusive) and max (exclusive)
function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min;
}

Demo with Range

带范围的演示

function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min;
}


function appendToResult(value) {
  document.getElementById("result").innerHTML += value + "<br />";
}

document.getElementById("result").innerHTML = "";

for (var i = 0; i < 10; i += 1) {
  appendToResult(getRandomArbitrary(-0.35, 0.35));
}
<pre id="result" />

This works because, the expression

这是因为,表达式

Math.random() * (max - min) + min

will be evaluated in the following manner. First,

将按以下方式评估。第一,

Math.random()

will be evaluated to get a random number in the range 0 and 1 and then

将被评估以获得0和1范围内的随机数,然后

Math.random() * (max - min)

it will be multiplied with the difference between max and min value. In your case, lets say max is 0.35 and min is -0.35. So, max - min becomes 0.7. So, Math.random() * (max - min) gives a number in the range 0 and 0.7 (because even if Math.random() produces 1, when you multiply it with 0.7, the result will become 0.7). Then, you do

它将乘以最大值和最小值之间的差值。在你的情况下,假设max为0.35,min为-0.35。所以,max - min变为0.7。因此,Math.random()*(max - min)给出一个0和0.7范围内的数字(因为即使Math.random()产生1,当你将它乘以0.7时,结果将变为0.7)。然后,你这样做

Math.random() * (max - min) + min

So, for your case, that basically becomes,

所以,对于你的情况,这基本上变成了,

Math.random() * (0.35 - (-0.35)) + (-0.35)
(Math.random() * 0.7) - 0.35

So, if (Math.random() * 0.7) generates any value greater than 0.35, since you subtract 0.35 from it, it becomes some number between 0 and 0.35. If (Math.random() * 0.7) generates a value lesser than 0.35, since you subtract 0.35 from it, it becomes a number between -0.35 and 0.

因此,如果(Math.random()* 0.7)生成任何大于0.35的值,因为从中减去0.35,它将变为介于0和0.35之间的某个数字。如果(Math.random()* 0.7)生成的值小于0.35,因为从中减去0.35,它将变为介于-0.35和0之间的数字。

#2


You can use Math.random() to get a number between 0 and 1 then reduce .5 to set the lower and upper bounds

您可以使用Math.random()来获取0到1之间的数字,然后减少.5来设置下限和上限

Math.random() - .5