如何使用实现Alea生成更好的随机数

时间:2011-12-18 19:36:09

标签: javascript random

我正在尝试将http://baagoe.com/en/RandomMusings/javascript/Alea.js实施到网站上,这样我就可以生成更好的随机数,但我似乎无法使其发挥作用。文档在这里:http://baagoe.com/en/RandomMusings/javascript/

文档声明要调用这个函数:

var random = Alea();

random(); // returns 0.6198398587293923

但它不起作用。

甚至只是声明var random = Alea();打破页面上的JavaScript。有什么我想念的吗?

这是我正在测试的完整页面:

<html>
<head>
<title>javascript test</title>
</head>
<body>
<h1>example</h1>

<script type="text/javascript">
// From http://baagoe.com/en/RandomMusings/javascript/
function Alea() {
  return (function(args) {
    // Johannes Baagøe <baagoe@baagoe.com>, 2010
    var s0 = 0;
    var s1 = 0;
    var s2 = 0;
    var c = 1;

    if (args.length == 0) {
      args = [+new Date];
    }
    var mash = Mash();
    s0 = mash(' ');
    s1 = mash(' ');
    s2 = mash(' ');

    for (var i = 0; i < args.length; i++) {
      s0 -= mash(args[i]);
      if (s0 < 0) {
        s0 += 1;
      }
      s1 -= mash(args[i]);
      if (s1 < 0) {
        s1 += 1;
      }
      s2 -= mash(args[i]);
      if (s2 < 0) {
        s2 += 1;
      }
    }
    mash = null;

    var random = function() {
      var t = 2091639 * s0 + c * 2.3283064365386963e-10; // 2^-32
      s0 = s1;
      s1 = s2;
      return s2 = t - (c = t | 0);
    };
    random.uint32 = function() {
      return random() * 0x100000000; // 2^32
    };
    random.fract53 = function() {
      return random() + 
        (random() * 0x200000 | 0) * 1.1102230246251565e-16; // 2^-53
    };
    random.version = 'Alea 0.9';
    random.args = args;
    return random;

  } (Array.prototype.slice.call(arguments)));
};

var random = Alea();

alert('test');

</script>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

here

问题是你没有包含Mash()函数,如{常见实现细节'下的article所述

答案 1 :(得分:0)

作为对此的补充,我不确定我是否正确理解其用法。我以为那是Math.random的替代品

我创建的页面是

<html>
<head>
<title>javascript test</title>
</head>
<body>
<h1>example</h1>
<script type="text/javascript">
// From http://baagoe.com/en/RandomMusings/javascript/
// http://baagoe.org/en/wiki/Better_random_numbers_for_javascript
function Alea() {
    return (function(args) {
    // Johannes Baagøe <baagoe@baagoe.com>, 2010
    var s0 = 0;
    var s1 = 0;
    var s2 = 0;
    var c = 1;

    if (args.length == 0) {
      args = [+new Date];
    }
    var mash = Mash();
    s0 = mash(' ');
    s1 = mash(' ');
    s2 = mash(' ');

    for (var i = 0; i < args.length; i++) {
      s0 -= mash(args[i]);
      if (s0 < 0) {
        s0 += 1;
      }
      s1 -= mash(args[i]);
      if (s1 < 0) {
        s1 += 1;
      }
  s2 -= mash(args[i]);
      if (s2 < 0) {
        s2 += 1;
      }
    }
mash = null;

var random = function() {
  var t = 2091639 * s0 + c * 2.3283064365386963e-10; // 2^-32
  s0 = s1;
  s1 = s2;
  return s2 = t - (c = t | 0);
};
random.uint32 = function() {
  return random() * 0x100000000; // 2^32
};
random.fract53 = function() {
  return random() + 
    (random() * 0x200000 | 0) * 1.1102230246251565e-16; // 2^-53
};
random.version = 'Alea 0.9';
random.args = args;
return random;

} (Array.prototype.slice.call(arguments)));
};

// From http://baagoe.com/en/RandomMusings/javascript/
// Johannes Baagøe <baagoe@baagoe.com>, 2010

function Mash() {
  var n = 0xefc8249d;

  var mash = function(data) {
    data = data.toString();
    for (var i = 0; i < data.length; i++) {
      n += data.charCodeAt(i);
  var h = 0.02519603282416938 * n;
  n = h >>> 0;
  h -= n;
  h *= n;
  n = h >>> 0;
  h -= n;
  n += h * 0x100000000; // 2^32
}
return (n >>> 0) * 2.3283064365386963e-10; // 2^-32
};

mash.version = 'Mash 0.9';
return mash;
}

var random = Alea();
alert('random = ' + random);
</script>
</body>
</html> 

我得到的警告说: -

random = function() {
  var t = 2091639 * s0 + c * 2.3283064365386963e-10; // 2^-32
  s0 = s1;
  s1 = s2;
  return s2 = t - (c = t | 0);
};