|
1 | | -// seedrandom.js version 2.0. |
2 | | -// Author: David Bau 4/2/2011 |
3 | | -// |
4 | | -// Defines a method Math.seedrandom() that, when called, substitutes |
5 | | -// an explicitly seeded RC4-based algorithm for Math.random(). Also |
6 | | -// supports automatic seeding from local or network sources of entropy. |
7 | | -// |
8 | | -// Usage: |
9 | | -// |
10 | | -// <script src=http://davidbau.com/encode/seedrandom-min.js></script> |
11 | | -// |
12 | | -// Math.seedrandom('yipee'); Sets Math.random to a function that is |
13 | | -// initialized using the given explicit seed. |
14 | | -// |
15 | | -// Math.seedrandom(); Sets Math.random to a function that is |
16 | | -// seeded using the current time, dom state, |
17 | | -// and other accumulated local entropy. |
18 | | -// The generated seed string is returned. |
19 | | -// |
20 | | -// Math.seedrandom('yowza', true); |
21 | | -// Seeds using the given explicit seed mixed |
22 | | -// together with accumulated entropy. |
23 | | -// |
24 | | -// <script src="http://bit.ly/srandom-512"></script> |
25 | | -// Seeds using physical random bits downloaded |
26 | | -// from random.org. |
27 | | -// |
28 | | -// <script src="https://jsonlib.appspot.com/urandom?callback=Math.seedrandom"> |
29 | | -// </script> Seeds using urandom bits from call.jsonlib.com, |
30 | | -// which is faster than random.org. |
31 | | -// |
32 | | -// Examples: |
33 | | -// |
34 | | -// Math.seedrandom("hello"); // Use "hello" as the seed. |
35 | | -// document.write(Math.random()); // Always 0.5463663768140734 |
36 | | -// document.write(Math.random()); // Always 0.43973793770592234 |
37 | | -// var rng1 = Math.random; // Remember the current prng. |
38 | | -// |
39 | | -// var autoseed = Math.seedrandom(); // New prng with an automatic seed. |
40 | | -// document.write(Math.random()); // Pretty much unpredictable. |
41 | | -// |
42 | | -// Math.random = rng1; // Continue "hello" prng sequence. |
43 | | -// document.write(Math.random()); // Always 0.554769432473455 |
44 | | -// |
45 | | -// Math.seedrandom(autoseed); // Restart at the previous seed. |
46 | | -// document.write(Math.random()); // Repeat the 'unpredictable' value. |
47 | | -// |
48 | | -// Notes: |
49 | | -// |
50 | | -// Each time seedrandom('arg') is called, entropy from the passed seed |
51 | | -// is accumulated in a pool to help generate future seeds for the |
52 | | -// zero-argument form of Math.seedrandom, so entropy can be injected over |
53 | | -// time by calling seedrandom with explicit data repeatedly. |
54 | | -// |
55 | | -// On speed - This javascript implementation of Math.random() is about |
56 | | -// 3-10x slower than the built-in Math.random() because it is not native |
57 | | -// code, but this is typically fast enough anyway. Seeding is more expensive, |
58 | | -// especially if you use auto-seeding. Some details (timings on Chrome 4): |
59 | | -// |
60 | | -// Our Math.random() - avg less than 0.002 milliseconds per call |
61 | | -// seedrandom('explicit') - avg less than 0.5 milliseconds per call |
62 | | -// seedrandom('explicit', true) - avg less than 2 milliseconds per call |
63 | | -// seedrandom() - avg about 38 milliseconds per call |
64 | | -// |
65 | | -// LICENSE (BSD): |
66 | | -// |
67 | | -// Copyright 2010 David Bau, all rights reserved. |
68 | | -// |
69 | | -// Redistribution and use in source and binary forms, with or without |
70 | | -// modification, are permitted provided that the following conditions are met: |
71 | | -// |
72 | | -// 1. Redistributions of source code must retain the above copyright |
73 | | -// notice, this list of conditions and the following disclaimer. |
74 | | -// |
75 | | -// 2. Redistributions in binary form must reproduce the above copyright |
76 | | -// notice, this list of conditions and the following disclaimer in the |
77 | | -// documentation and/or other materials provided with the distribution. |
78 | | -// |
79 | | -// 3. Neither the name of this module nor the names of its contributors may |
80 | | -// be used to endorse or promote products derived from this software |
81 | | -// without specific prior written permission. |
82 | | -// |
83 | | -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
84 | | -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
85 | | -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
86 | | -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
87 | | -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
88 | | -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
89 | | -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
90 | | -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
91 | | -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
92 | | -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
93 | | -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
94 | | -// |
| 1 | +/** |
| 2 | + * @fileoverview |
| 3 | + * seedrandom.js version 2.0. |
| 4 | + * Author: David Bau 4/2/2011 |
| 5 | + * |
| 6 | + * Defines a method Math.seedrandom() that, when called, substitutes |
| 7 | + * an explicitly seeded RC4-based algorithm for Math.random(). Also |
| 8 | + * supports automatic seeding from local or network sources of entropy. |
| 9 | + * |
| 10 | + * Usage: |
| 11 | + * |
| 12 | + * <script src=http://davidbau.com/encode/seedrandom-min.js></script> |
| 13 | + * |
| 14 | + * Math.seedrandom('yipee'); Sets Math.random to a function that is |
| 15 | + * initialized using the given explicit seed. |
| 16 | + * |
| 17 | + * Math.seedrandom(); Sets Math.random to a function that is |
| 18 | + * seeded using the current time, dom state, |
| 19 | + * and other accumulated local entropy. |
| 20 | + * The generated seed string is returned. |
| 21 | + * |
| 22 | + * Math.seedrandom('yowza', true); |
| 23 | + * Seeds using the given explicit seed mixed |
| 24 | + * together with accumulated entropy. |
| 25 | + * |
| 26 | + * <script src="http://bit.ly/srandom-512"></script> |
| 27 | + * Seeds using physical random bits downloaded |
| 28 | + * from random.org. |
| 29 | + * |
| 30 | + * <script src="https://jsonlib.appspot.com/urandom?callback=Math.seedrandom"> |
| 31 | + * </script> Seeds using urandom bits from call.jsonlib.com, |
| 32 | + * which is faster than random.org. |
| 33 | + * |
| 34 | + * Examples: |
| 35 | + * |
| 36 | + * Math.seedrandom("hello"); // Use "hello" as the seed. |
| 37 | + * document.write(Math.random()); // Always 0.5463663768140734 |
| 38 | + * document.write(Math.random()); // Always 0.43973793770592234 |
| 39 | + * var rng1 = Math.random; // Remember the current prng. |
| 40 | + * |
| 41 | + * var autoseed = Math.seedrandom(); // New prng with an automatic seed. |
| 42 | + * document.write(Math.random()); // Pretty much unpredictable. |
| 43 | + * |
| 44 | + * Math.random = rng1; // Continue "hello" prng sequence. |
| 45 | + * document.write(Math.random()); // Always 0.554769432473455 |
| 46 | + * |
| 47 | + * Math.seedrandom(autoseed); // Restart at the previous seed. |
| 48 | + * document.write(Math.random()); // Repeat the 'unpredictable' value. |
| 49 | + * |
| 50 | + * Notes: |
| 51 | + * |
| 52 | + * Each time seedrandom('arg') is called, entropy from the passed seed |
| 53 | + * is accumulated in a pool to help generate future seeds for the |
| 54 | + * zero-argument form of Math.seedrandom, so entropy can be injected over |
| 55 | + * time by calling seedrandom with explicit data repeatedly. |
| 56 | + * |
| 57 | + * On speed - This javascript implementation of Math.random() is about |
| 58 | + * 3-10x slower than the built-in Math.random() because it is not native |
| 59 | + * code, but this is typically fast enough anyway. Seeding is more expensive, |
| 60 | + * especially if you use auto-seeding. Some details (timings on Chrome 4): |
| 61 | + * |
| 62 | + * Our Math.random() - avg less than 0.002 milliseconds per call |
| 63 | + * seedrandom('explicit') - avg less than 0.5 milliseconds per call |
| 64 | + * seedrandom('explicit', true) - avg less than 2 milliseconds per call |
| 65 | + * seedrandom() - avg about 38 milliseconds per call |
| 66 | + * |
| 67 | + * @license |
| 68 | + * seedrandom.js |
| 69 | + * Copyright 2010 David Bau, all rights reserved. |
| 70 | + * |
| 71 | + * Redistribution and use in source and binary forms, with or without |
| 72 | + * modification, are permitted provided that the following conditions are met: |
| 73 | + * |
| 74 | + * 1. Redistributions of source code must retain the above copyright |
| 75 | + * notice, this list of conditions and the following disclaimer. |
| 76 | + * |
| 77 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 78 | + * notice, this list of conditions and the following disclaimer in the |
| 79 | + * documentation and/or other materials provided with the distribution. |
| 80 | + * |
| 81 | + * 3. Neither the name of this module nor the names of its contributors may |
| 82 | + * be used to endorse or promote products derived from this software |
| 83 | + * without specific prior written permission. |
| 84 | + * |
| 85 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 86 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 87 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 88 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 89 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 90 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 91 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 92 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 93 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 94 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 95 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 96 | + */ |
| 97 | + |
95 | 98 | /** |
96 | 99 | * All code is in an anonymous closure to keep the global namespace clean. |
97 | 100 | * |
|
0 commit comments