【问题标题】:Getting a "random" number from 0 - 1 depending on a number and a count [duplicate]根据数字和计数从0-1获取“随机”数字[重复]
【发布时间】: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 来计算“随机”数!

标签: javascript math numbers random-seed


【解决方案1】:

正如 Joel Lee 所说,这是

的副本

Seedable JavaScript random number generator.

David Bau 的回答似乎正是我所需要的:

http://davidbau.com/seedrandom

“种子”也可以是一个字符串。

我很抱歉重复。

【讨论】:

    猜你喜欢
    • 2012-06-22
    • 2015-01-08
    • 2016-12-25
    • 2017-05-28
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 2014-06-19
    • 2021-09-27
    相关资源
    最近更新 更多