Rev | Line | |
---|
[483] | 1 | // AMD-ID "dojox/math/random/prng4" |
---|
| 2 | define(["dojo", "dojox"], function(dojo, dojox) { |
---|
| 3 | |
---|
| 4 | dojo.getObject("math.random.prng4", true, dojox); |
---|
| 5 | |
---|
| 6 | // Copyright (c) 2005 Tom Wu |
---|
| 7 | // All Rights Reserved. |
---|
| 8 | // See "LICENSE-BigInteger" for details. |
---|
| 9 | |
---|
| 10 | // prng4.js - uses Arcfour as a PRNG |
---|
| 11 | |
---|
| 12 | function Arcfour() { |
---|
| 13 | this.i = 0; |
---|
| 14 | this.j = 0; |
---|
| 15 | this.S = new Array(256); |
---|
| 16 | } |
---|
| 17 | |
---|
| 18 | dojo.extend(Arcfour, { |
---|
| 19 | init: function(key){ |
---|
| 20 | // summary: |
---|
| 21 | // Initialize arcfour context |
---|
| 22 | // key: int[] |
---|
| 23 | // an array of ints, each from [0..255] |
---|
| 24 | var i, j, t, S = this.S, len = key.length; |
---|
| 25 | for(i = 0; i < 256; ++i){ |
---|
| 26 | S[i] = i; |
---|
| 27 | } |
---|
| 28 | j = 0; |
---|
| 29 | for(i = 0; i < 256; ++i){ |
---|
| 30 | j = (j + S[i] + key[i % len]) & 255; |
---|
| 31 | t = S[i]; |
---|
| 32 | S[i] = S[j]; |
---|
| 33 | S[j] = t; |
---|
| 34 | } |
---|
| 35 | this.i = 0; |
---|
| 36 | this.j = 0; |
---|
| 37 | }, |
---|
| 38 | |
---|
| 39 | next: function(){ |
---|
| 40 | var t, i, j, S = this.S; |
---|
| 41 | this.i = i = (this.i + 1) & 255; |
---|
| 42 | this.j = j = (this.j + S[i]) & 255; |
---|
| 43 | t = S[i]; |
---|
| 44 | S[i] = S[j]; |
---|
| 45 | S[j] = t; |
---|
| 46 | return S[(t + S[i]) & 255]; |
---|
| 47 | } |
---|
| 48 | }); |
---|
| 49 | |
---|
| 50 | dojox.math.random.prng4 = function(){ |
---|
| 51 | return new Arcfour(); |
---|
| 52 | }; |
---|
| 53 | |
---|
| 54 | // Pool size must be a multiple of 4 and greater than 32. |
---|
| 55 | // An array of bytes the size of the pool will be passed to init() |
---|
| 56 | dojox.math.random.prng4.size = 256; |
---|
| 57 | |
---|
| 58 | return dojox.math.random.prng4; |
---|
| 59 | }); |
---|
Note: See
TracBrowser
for help on using the repository browser.