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