1 Clazz.load(null,"java.util.Random",["java.lang.IllegalArgumentException"],function(){
2 c$=Clazz.decorateAsClass(function(){
3 this.haveNextNextGaussian=false;
5 this.nextNextGaussian=0;
6 Clazz.instantialize(this,arguments);
7 },java.util,"Random",null,java.io.Serializable);
8 Clazz.makeConstructor(c$,
10 this.setSeed(System.currentTimeMillis());
12 Clazz.makeConstructor(c$,
16 Clazz.defineMethod(c$,"next",
18 this.seed=(this.seed*25214903917+0xb)&(281474976710655);
19 return(this.seed>>>(48-bits));
21 Clazz.defineMethod(c$,"nextBoolean",
23 return Math.random()>0.5;
25 Clazz.defineMethod(c$,"nextBytes",
27 for(var i=0;i<bytes.length;i++){
28 bytes[i]=Math.round(0x100*Math.random());
31 Clazz.defineMethod(c$,"nextDouble",
35 Clazz.defineMethod(c$,"nextFloat",
39 Clazz.defineMethod(c$,"nextGaussian",
41 if(this.haveNextNextGaussian){
42 this.haveNextNextGaussian=false;
43 return this.nextNextGaussian;
48 v1=2*this.nextDouble()-1;
49 v2=2*this.nextDouble()-1;
52 var norm=Math.sqrt(-2*Math.log(s)/s);
53 this.nextNextGaussian=v2*norm;
54 this.haveNextNextGaussian=true;
57 Clazz.defineMethod(c$,"nextInt",
59 return Math.ceil(0xffff*Math.random())-0x8000;
61 Clazz.defineMethod(c$,"nextInt",
65 return Math.floor((2 << (n - 1)) * Math.random())
69 return((n*this.next(31))>>31);
75 }while(bits-val+(n-1)<0);
82 throw new IllegalArgumentException();
84 Clazz.defineMethod(c$,"nextLong",
86 return Math.ceil(0xffffffff*Math.random())-0x80000000;
88 Clazz.defineMethod(c$,"setSeed",
90 Math.seedrandom(seed);
91 //this.seed=(seed^25214903917)&(281474976710655);
92 //this.haveNextNextGaussian=false;
94 Clazz.defineStatics(c$,
95 "multiplier",0x5deece66d);
99 // Author: David Bau 3/11/2010
101 // Defines a method Math.seedrandom() that, when called, substitutes
102 // an explicitly seeded RC4-based algorithm for Math.random(). Also
103 // supports automatic seeding from local or network sources of entropy.
107 // <script src=http://davidbau.com/encode/seedrandom-min.js></script>
109 // Math.seedrandom('yipee'); Sets Math.random to a function that is
110 // initialized using the given explicit seed.
112 // Math.seedrandom(); Sets Math.random to a function that is
113 // seeded using the current time, dom state,
114 // and other accumulated local entropy.
115 // The generated seed string is returned.
117 // Math.seedrandom('yowza', true);
118 // Seeds using the given explicit seed mixed
119 // together with accumulated entropy.
121 // <script src="http://bit.ly/srandom-512"></script>
122 // Seeds using physical random bits downloaded
127 // Math.seedrandom("hello"); // Use "hello" as the seed.
128 // document.write(Math.random()); // Always 0.5463663768140734
129 // document.write(Math.random()); // Always 0.43973793770592234
130 // var rng1 = Math.random; // Remember the current prng.
132 // var autoseed = Math.seedrandom(); // New prng with an automatic seed.
133 // document.write(Math.random()); // Pretty much unpredictable.
135 // Math.random = rng1; // Continue "hello" prng sequence.
136 // document.write(Math.random()); // Always 0.554769432473455
138 // Math.seedrandom(autoseed); // Restart at the previous seed.
139 // document.write(Math.random()); // Repeat the 'unpredictable' value.
143 // Each time seedrandom('arg') is called, entropy from the passed seed
144 // is accumulated in a pool to help generate future seeds for the
145 // zero-argument form of Math.seedrandom, so entropy can be injected over
146 // time by calling seedrandom with explicit data repeatedly.
148 // On speed - This javascript implementation of Math.random() is about
149 // 3-10x slower than the built-in Math.random() because it is not native
150 // code, but this is typically fast enough anyway. Seeding is more expensive,
151 // especially if you use auto-seeding. Some details (timings on Chrome 4):
153 // Our Math.random() - avg less than 0.002 milliseconds per call
154 // seedrandom('explicit') - avg less than 0.5 milliseconds per call
155 // seedrandom('explicit', true) - avg less than 2 milliseconds per call
156 // seedrandom() - avg about 38 milliseconds per call
160 // Copyright 2010 David Bau, all rights reserved.
162 // Redistribution and use in source and binary forms, with or without
163 // modification, are permitted provided that the following conditions are met:
165 // 1. Redistributions of source code must retain the above copyright
166 // notice, this list of conditions and the following disclaimer.
168 // 2. Redistributions in binary form must reproduce the above copyright
169 // notice, this list of conditions and the following disclaimer in the
170 // documentation and/or other materials provided with the distribution.
172 // 3. Neither the name of this module nor the names of its contributors may
173 // be used to endorse or promote products derived from this software
174 // without specific prior written permission.
176 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
177 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
178 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
179 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
180 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
181 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
182 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
183 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
184 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
185 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
186 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
189 * All code is in an anonymous closure to keep the global namespace clean.
191 * @param {number=} overflow
192 * @param {number=} startdenom
194 (function (pool, math, width, chunks, significance, overflow, startdenom) {
196 var copyright = "Copyright 2010 David Bau, all rights reserved. (BSD)"
199 // This is the seedrandom function described above.
201 math['seedrandom'] = function seedrandom(seed, use_entropy) {
205 // Flatten the seed string or build one from local entropy if needed.
206 seed = mixkey(flatten(
207 use_entropy ? [seed, pool] :
208 arguments.length ? seed :
209 [new Date().getTime(), pool, window], 3), key);
211 // Use the seed to initialize an ARC4 generator.
212 arc4 = new ARC4(key);
214 // Mix the randomness into accumulated entropy.
215 mixkey(arc4.S, pool);
217 // Override Math.random
219 // This function returns a random double in [0, 1) that contains
220 // randomness in every bit of the mantissa of the IEEE 754 value.
222 math['random'] = function random() { // Closure to return a random double:
223 var n = arc4.g(chunks); // Start with a numerator n < 2 ^ 48
224 var d = startdenom; // and denominator d = 2 ^ 48.
225 var x = 0; // and no 'extra last byte'.
226 while (n < significance) { // Fill up all significant digits by
227 n = (n + x) * width; // shifting numerator and
228 d *= width; // denominator and generating a
229 x = arc4.g(1); // new least-significant-byte.
231 while (n >= overflow) { // To avoid rounding up, before adding
232 n /= 2; // last byte, shift everything
233 d /= 2; // right using integer math until
234 x >>>= 1; // we have exactly the desired bits.
236 return (n + x) / d; // Form the number within [0, 1).
239 // Return the seed that was used
246 // An ARC4 implementation. The constructor takes a key in the form of
247 // an array of at most (width) integers that should be 0 <= x < (width).
249 // The g(count) method returns a pseudorandom integer that concatenates
250 // the next (count) outputs from ARC4. Its return value is a number x
251 // that is in the range 0 <= x < (width ^ count).
255 var t, u, me = this, keylen = key.length;
256 var i = 0, j = me.i = me.j = me.m = 0;
260 // The empty key [] is treated as [0].
261 if (!keylen) { key = [keylen++]; }
263 // Set up S using the standard key scheduling algorithm.
264 while (i < width) { me.S[i] = i++; }
265 for (i = 0; i < width; i++) {
267 j = lowbits(j + t + key[i % keylen]);
273 // The "g" method returns the next (count) outputs as one number.
274 me.g = function getnext(count) {
276 var i = lowbits(me.i + 1); var t = s[i];
277 var j = lowbits(me.j + t); var u = s[j];
280 var r = s[lowbits(t + u)];
282 i = lowbits(i + 1); t = s[i];
283 j = lowbits(j + t); u = s[j];
286 r = r * width + s[lowbits(t + u)];
292 // For robust unpredictability discard an initial batch of values.
293 // See http://www.rsa.com/rsalabs/node.asp?id=2009
299 // Converts an object tree to nested arrays of strings.
301 /** @param {Object=} result
302 * @param {string=} prop */
303 function flatten(obj, depth, result, prop) {
305 if (depth && typeof(obj) == 'object') {
307 if (prop.indexOf('S') < 5) { // Avoid FF3 bug (local/sessionStorage)
308 try { result.push(flatten(obj[prop], depth - 1)); } catch (e) {}
312 return result.length ? result : '' + obj;
317 // Mixes a string seed into a key that is an array of integers, and
318 // returns a shortened string seed that is equivalent to the result key.
320 /** @param {number=} smear
321 * @param {number=} j */
322 function mixkey(seed, key, smear, j) {
323 seed += ''; // Ensure the seed is a string
325 for (j = 0; j < seed.length; j++) {
327 lowbits((smear ^= key[lowbits(j)] * 19) + seed.charCodeAt(j));
330 for (j in key) { seed += String.fromCharCode(key[j]); }
336 // A quick "n mod width" for width a power of 2.
338 function lowbits(n) { return n & (width - 1); }
341 // The following constants are related to IEEE 754 limits.
343 startdenom = math.pow(width, chunks);
344 significance = math.pow(2, significance);
345 overflow = significance * 2;
348 // When seedrandom.js is loaded, we immediately mix a few bits
349 // from the built-in RNG into the entropy pool. Because we do
350 // not want to intefere with determinstic PRNG state later,
351 // seedrandom will not call math.random on its own again after
354 mixkey(math.random(), pool);
356 // End anonymous scope, and pass initial values.
358 [], // pool: entropy pool starts empty
359 Math, // math: package containing random, pow, and seedrandom
360 256, // width: each RC4 output is 0 <= x < 256
361 6, // chunks: at least six RC4 outputs for each double
362 52 // significance: there are 52 significant digits in a double