【发布时间】:2013-09-11 14:35:21
【问题描述】:
我有一个问题,尽管我认为它更多的是数学而不是编程。很抱歉,如果这是错误的发布位置。
但我想在 Javascript 中创建一个可以执行以下操作的函数:
- 函数有一个种子参数
- 函数内部有一个循环。
- 它必须返回一个介于 0 和 1 之间的数字,具体取决于计数和种子。
如果我有这个例子:
var numbers = giveMeNumbers(3224425, 10);
应该给我 10 个介于 0 和 1 之间的“随机”数字。但它必须与每次运行此函数时返回的数字相同(使用相同的种子代码)
function giveMeNumbers(seed, amount) {
var array = [];
for (var i=0;i<amount;i++)
{
var result = 0; //some cool calculation that always gives the same result
//example: though a bad example.
result = seed * i % 6;
//here I use i to calculate a number but the problem is that i dont want it to be systematic. i mean i only get increasing numbers etc.
array.push(result);
}
return array;
}
所以总而言之,我想要的是返回一个不增加(或系统)的数字。一个完美的结果可能是数字:
0.3345 0.8563 0.2234 0.8594 0.1231 等等
因此,数字中没有系统或增量。
【问题讨论】:
-
所以你的意思是你想要一个随机序列,但不包括一个增加值的序列,但其他情况(如所有减少)可以吗? (随机通常意味着它可能是任何东西,并且可能会增加。)
-
如果你乘以一个以已知方式增加的数字,它怎么会是随机的。您不应该使用 i 来计算“随机”数!
-
@JohnMalkowich 你看过the Wikipedia page about random number generation?
标签: javascript math numbers random-seed