11a3812139a3d820475d2c279cea310b8fed29c2
[jalviewjs.git] / site / swingjs / j2s / java / util / Random.js
1 Clazz.load(null,"java.util.Random",["java.lang.IllegalArgumentException"],function(){
2 c$=Clazz.decorateAsClass(function(){
3 this.haveNextNextGaussian=false;
4 this.seed=0;
5 this.nextNextGaussian=0;
6 Clazz.instantialize(this,arguments);
7 },java.util,"Random",null,java.io.Serializable);
8 Clazz.makeConstructor(c$,
9 function(){
10 this.setSeed(System.currentTimeMillis());
11 });
12 Clazz.makeConstructor(c$,
13 function(seed){
14 this.setSeed(seed);
15 },"~N");
16 Clazz.defineMethod(c$,"next",
17 function(bits){
18 this.seed=(this.seed*25214903917+0xb)&(281474976710655);
19 return(this.seed>>>(48-bits));
20 },"~N");
21 Clazz.defineMethod(c$,"nextBoolean",
22 function(){
23 return Math.random()>0.5;
24 });
25 Clazz.defineMethod(c$,"nextBytes",
26 function(buf){
27 for(var i=0;i<bytes.length;i++){
28 bytes[i]=Math.round(0x100*Math.random());
29 }
30 },"~A");
31 Clazz.defineMethod(c$,"nextDouble",
32 function(){
33 return Math.random();
34 });
35 Clazz.defineMethod(c$,"nextFloat",
36 function(){
37 return Math.random();
38 });
39 Clazz.defineMethod(c$,"nextGaussian",
40 function(){
41 if(this.haveNextNextGaussian){
42 this.haveNextNextGaussian=false;
43 return this.nextNextGaussian;
44 }var v1;
45 var v2;
46 var s;
47 do{
48 v1=2*this.nextDouble()-1;
49 v2=2*this.nextDouble()-1;
50 s=v1*v1+v2*v2;
51 }while(s>=1);
52 var norm=Math.sqrt(-2*Math.log(s)/s);
53 this.nextNextGaussian=v2*norm;
54 this.haveNextNextGaussian=true;
55 return v1*norm;
56 });
57 Clazz.defineMethod(c$,"nextInt",
58 function(){
59 return Math.ceil(0xffff*Math.random())-0x8000;
60 });
61 Clazz.defineMethod(c$,"nextInt",
62 function(n){
63 if(n>0){
64 n = Math.min(n, 31);
65 return Math.floor((2 << (n - 1)) * Math.random())
66
67 /*
68 if((n&-n)==n){
69 return((n*this.next(31))>>31);
70 }var bits;
71 var val;
72 do{
73 bits=this.next(31);
74 val=bits%n;
75 }while(bits-val+(n-1)<0);
76
77
78 return val;
79
80 */
81 }
82 throw new IllegalArgumentException();
83 },"~N");
84 Clazz.defineMethod(c$,"nextLong",
85 function(){
86 return Math.ceil(0xffffffff*Math.random())-0x80000000;
87 });
88 Clazz.defineMethod(c$,"setSeed",
89 function(seed){
90 Math.seedrandom(seed);
91 //this.seed=(seed^25214903917)&(281474976710655);
92 //this.haveNextNextGaussian=false;
93 },"~N");
94 Clazz.defineStatics(c$,
95 "multiplier",0x5deece66d);
96 });
97
98 // seedrandom.js
99 // Author: David Bau 3/11/2010
100 //
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.
104 //
105 // Usage:
106 //
107 //   <script src=http://davidbau.com/encode/seedrandom-min.js></script>
108 //
109 //   Math.seedrandom('yipee'); Sets Math.random to a function that is
110 //                             initialized using the given explicit seed.
111 //
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.
116 //
117 //   Math.seedrandom('yowza', true);
118 //                             Seeds using the given explicit seed mixed
119 //                             together with accumulated entropy.
120 //
121 //   <script src="http://bit.ly/srandom-512"></script>
122 //                             Seeds using physical random bits downloaded
123 //                             from random.org.
124 //
125 // Examples:
126 //
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.
131 //
132 //   var autoseed = Math.seedrandom();    // New prng with an automatic seed.
133 //   document.write(Math.random());       // Pretty much unpredictable.
134 //
135 //   Math.random = rng1;                  // Continue "hello" prng sequence.
136 //   document.write(Math.random());       // Always 0.554769432473455
137 //
138 //   Math.seedrandom(autoseed);           // Restart at the previous seed.
139 //   document.write(Math.random());       // Repeat the 'unpredictable' value.
140 //
141 // Notes:
142 //
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.
147 //
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):
152 //
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
157 //
158 // LICENSE (BSD):
159 //
160 // Copyright 2010 David Bau, all rights reserved.
161 //
162 // Redistribution and use in source and binary forms, with or without
163 // modification, are permitted provided that the following conditions are met:
164 //
165 //   1. Redistributions of source code must retain the above copyright
166 //      notice, this list of conditions and the following disclaimer.
167 //
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.
171 //
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.
175 //
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.
187 //
188 /**
189  * All code is in an anonymous closure to keep the global namespace clean.
190  *
191  * @param {number=} overflow
192  * @param {number=} startdenom
193  */
194 (function (pool, math, width, chunks, significance, overflow, startdenom) {
195
196 var copyright = "Copyright 2010 David Bau, all rights reserved. (BSD)"
197 //
198 // seedrandom()
199 // This is the seedrandom function described above.
200 //
201 math['seedrandom'] = function seedrandom(seed, use_entropy) {
202   var key = [];
203   var arc4;
204
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);
210
211   // Use the seed to initialize an ARC4 generator.
212   arc4 = new ARC4(key);
213
214   // Mix the randomness into accumulated entropy.
215   mixkey(arc4.S, pool);
216
217   // Override Math.random
218
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.
221
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.
230     }
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.
235     }
236     return (n + x) / d;                 // Form the number within [0, 1).
237   };
238
239   // Return the seed that was used
240   return seed;
241 };
242
243 //
244 // ARC4
245 //
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).
248 //
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).
252 //
253 /** @constructor */
254 function ARC4(key) {
255   var t, u, me = this, keylen = key.length;
256   var i = 0, j = me.i = me.j = me.m = 0;
257   me.S = [];
258   me.c = [];
259
260   // The empty key [] is treated as [0].
261   if (!keylen) { key = [keylen++]; }
262
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++) {
266     t = me.S[i];
267     j = lowbits(j + t + key[i % keylen]);
268     u = me.S[j];
269     me.S[i] = u;
270     me.S[j] = t;
271   }
272
273   // The "g" method returns the next (count) outputs as one number.
274   me.g = function getnext(count) {
275     var s = me.S;
276     var i = lowbits(me.i + 1); var t = s[i];
277     var j = lowbits(me.j + t); var u = s[j];
278     s[i] = u;
279     s[j] = t;
280     var r = s[lowbits(t + u)];
281     while (--count) {
282       i = lowbits(i + 1); t = s[i];
283       j = lowbits(j + t); u = s[j];
284       s[i] = u;
285       s[j] = t;
286       r = r * width + s[lowbits(t + u)];
287     }
288     me.i = i;
289     me.j = j;
290     return r;
291   };
292   // For robust unpredictability discard an initial batch of values.
293   // See http://www.rsa.com/rsalabs/node.asp?id=2009
294   me.g(width);
295 }
296
297 //
298 // flatten()
299 // Converts an object tree to nested arrays of strings.
300 //
301 /** @param {Object=} result
302   * @param {string=} prop */
303 function flatten(obj, depth, result, prop) {
304   result = [];
305   if (depth && typeof(obj) == 'object') {
306     for (prop in obj) {
307       if (prop.indexOf('S') < 5) {    // Avoid FF3 bug (local/sessionStorage)
308         try { result.push(flatten(obj[prop], depth - 1)); } catch (e) {}
309       }
310     }
311   }
312   return result.length ? result : '' + obj;
313 }
314
315 //
316 // mixkey()
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.
319 //
320 /** @param {number=} smear
321   * @param {number=} j */
322 function mixkey(seed, key, smear, j) {
323   seed += '';                         // Ensure the seed is a string
324   smear = 0;
325   for (j = 0; j < seed.length; j++) {
326     key[lowbits(j)] =
327       lowbits((smear ^= key[lowbits(j)] * 19) + seed.charCodeAt(j));
328   }
329   seed = '';
330   for (j in key) { seed += String.fromCharCode(key[j]); }
331   return seed;
332 }
333
334 //
335 // lowbits()
336 // A quick "n mod width" for width a power of 2.
337 //
338 function lowbits(n) { return n & (width - 1); }
339
340 //
341 // The following constants are related to IEEE 754 limits.
342 //
343 startdenom = math.pow(width, chunks);
344 significance = math.pow(2, significance);
345 overflow = significance * 2;
346
347 //
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
352 // initialization.
353 //
354 mixkey(math.random(), pool);
355
356 // End anonymous scope, and pass initial values.
357 })(
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
363 );
364