simple-squiggle

A restricted subset of Squiggle
Log | Files | Refs | README

alea.js (3238B)


      1 // A port of an algorithm by Johannes Baagøe <baagoe@baagoe.com>, 2010
      2 // http://baagoe.com/en/RandomMusings/javascript/
      3 // https://github.com/nquinlan/better-random-numbers-for-javascript-mirror
      4 // Original work is under MIT license -
      5 
      6 // Copyright (C) 2010 by Johannes Baagøe <baagoe@baagoe.org>
      7 //
      8 // Permission is hereby granted, free of charge, to any person obtaining a copy
      9 // of this software and associated documentation files (the "Software"), to deal
     10 // in the Software without restriction, including without limitation the rights
     11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     12 // copies of the Software, and to permit persons to whom the Software is
     13 // furnished to do so, subject to the following conditions:
     14 //
     15 // The above copyright notice and this permission notice shall be included in
     16 // all copies or substantial portions of the Software.
     17 //
     18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     24 // THE SOFTWARE.
     25 
     26 
     27 
     28 (function(global, module, define) {
     29 
     30 function Alea(seed) {
     31   var me = this, mash = Mash();
     32 
     33   me.next = function() {
     34     var t = 2091639 * me.s0 + me.c * 2.3283064365386963e-10; // 2^-32
     35     me.s0 = me.s1;
     36     me.s1 = me.s2;
     37     return me.s2 = t - (me.c = t | 0);
     38   };
     39 
     40   // Apply the seeding algorithm from Baagoe.
     41   me.c = 1;
     42   me.s0 = mash(' ');
     43   me.s1 = mash(' ');
     44   me.s2 = mash(' ');
     45   me.s0 -= mash(seed);
     46   if (me.s0 < 0) { me.s0 += 1; }
     47   me.s1 -= mash(seed);
     48   if (me.s1 < 0) { me.s1 += 1; }
     49   me.s2 -= mash(seed);
     50   if (me.s2 < 0) { me.s2 += 1; }
     51   mash = null;
     52 }
     53 
     54 function copy(f, t) {
     55   t.c = f.c;
     56   t.s0 = f.s0;
     57   t.s1 = f.s1;
     58   t.s2 = f.s2;
     59   return t;
     60 }
     61 
     62 function impl(seed, opts) {
     63   var xg = new Alea(seed),
     64       state = opts && opts.state,
     65       prng = xg.next;
     66   prng.int32 = function() { return (xg.next() * 0x100000000) | 0; }
     67   prng.double = function() {
     68     return prng() + (prng() * 0x200000 | 0) * 1.1102230246251565e-16; // 2^-53
     69   };
     70   prng.quick = prng;
     71   if (state) {
     72     if (typeof(state) == 'object') copy(state, xg);
     73     prng.state = function() { return copy(xg, {}); }
     74   }
     75   return prng;
     76 }
     77 
     78 function Mash() {
     79   var n = 0xefc8249d;
     80 
     81   var mash = function(data) {
     82     data = String(data);
     83     for (var i = 0; i < data.length; i++) {
     84       n += data.charCodeAt(i);
     85       var h = 0.02519603282416938 * n;
     86       n = h >>> 0;
     87       h -= n;
     88       h *= n;
     89       n = h >>> 0;
     90       h -= n;
     91       n += h * 0x100000000; // 2^32
     92     }
     93     return (n >>> 0) * 2.3283064365386963e-10; // 2^-32
     94   };
     95 
     96   return mash;
     97 }
     98 
     99 
    100 if (module && module.exports) {
    101   module.exports = impl;
    102 } else if (define && define.amd) {
    103   define(function() { return impl; });
    104 } else {
    105   this.alea = impl;
    106 }
    107 
    108 })(
    109   this,
    110   (typeof module) == 'object' && module,    // present in node.js
    111   (typeof define) == 'function' && define   // present with an AMD loader
    112 );
    113 
    114