|
|
Line 2,449: |
Line 2,449: |
| return ret; | | return ret; |
| } | | } |
- | // seedrandom.js version 2.0.
| |
- | // Author: David Bau 4/2/2011
| |
- | //
| |
- | // Defines a method Math.seedrandom() that, when called, substitutes
| |
- | // an explicitly seeded RC4-based algorithm for Math.random(). Also
| |
- | // supports automatic seeding from local or network sources of entropy.
| |
- | //
| |
- | // Usage:
| |
- | //
| |
- | // <script src=http://davidbau.com/encode/seedrandom-min.js></script>
| |
- | //
| |
- | // Math.seedrandom('yipee'); Sets Math.random to a function that is
| |
- | // initialized using the given explicit seed.
| |
- | //
| |
- | // Math.seedrandom(); Sets Math.random to a function that is
| |
- | // seeded using the current time, dom state,
| |
- | // and other accumulated local entropy.
| |
- | // The generated seed string is returned.
| |
- | //
| |
- | // Math.seedrandom('yowza', true);
| |
- | // Seeds using the given explicit seed mixed
| |
- | // together with accumulated entropy.
| |
- | //
| |
- | // <script src="http://bit.ly/srandom-512"></script>
| |
- | // Seeds using physical random bits downloaded
| |
- | // from random.org.
| |
- | //
| |
- | // <script src="https://jsonlib.appspot.com/urandom?callback=Math.seedrandom">
| |
- | // </script> Seeds using urandom bits from call.jsonlib.com,
| |
- | // which is faster than random.org.
| |
- | //
| |
- | // Examples:
| |
- | //
| |
- | // Math.seedrandom("hello"); // Use "hello" as the seed.
| |
- | // document.write(Math.random()); // Always 0.5463663768140734
| |
- | // document.write(Math.random()); // Always 0.43973793770592234
| |
- | // var rng1 = Math.random; // Remember the current prng.
| |
- | //
| |
- | // var autoseed = Math.seedrandom(); // New prng with an automatic seed.
| |
- | // document.write(Math.random()); // Pretty much unpredictable.
| |
- | //
| |
- | // Math.random = rng1; // Continue "hello" prng sequence.
| |
- | // document.write(Math.random()); // Always 0.554769432473455
| |
- | //
| |
- | // Math.seedrandom(autoseed); // Restart at the previous seed.
| |
- | // document.write(Math.random()); // Repeat the 'unpredictable' value.
| |
- | //
| |
- | // Notes:
| |
- | //
| |
- | // Each time seedrandom('arg') is called, entropy from the passed seed
| |
- | // is accumulated in a pool to help generate future seeds for the
| |
- | // zero-argument form of Math.seedrandom, so entropy can be injected over
| |
- | // time by calling seedrandom with explicit data repeatedly.
| |
- | //
| |
- | // On speed - This javascript implementation of Math.random() is about
| |
- | // 3-10x slower than the built-in Math.random() because it is not native
| |
- | // code, but this is typically fast enough anyway. Seeding is more expensive,
| |
- | // especially if you use auto-seeding. Some details (timings on Chrome 4):
| |
- | //
| |
- | // Our Math.random() - avg less than 0.002 milliseconds per call
| |
- | // seedrandom('explicit') - avg less than 0.5 milliseconds per call
| |
- | // seedrandom('explicit', true) - avg less than 2 milliseconds per call
| |
- | // seedrandom() - avg about 38 milliseconds per call
| |
- | //
| |
- | // LICENSE (BSD):
| |
- | //
| |
- | // Copyright 2010 David Bau, all rights reserved.
| |
- | //
| |
- | // Redistribution and use in source and binary forms, with or without
| |
- | // modification, are permitted provided that the following conditions are met:
| |
- | //
| |
- | // 1. Redistributions of source code must retain the above copyright
| |
- | // notice, this list of conditions and the following disclaimer.
| |
- | //
| |
- | // 2. Redistributions in binary form must reproduce the above copyright
| |
- | // notice, this list of conditions and the following disclaimer in the
| |
- | // documentation and/or other materials provided with the distribution.
| |
- | //
| |
- | // 3. Neither the name of this module nor the names of its contributors may
| |
- | // be used to endorse or promote products derived from this software
| |
- | // without specific prior written permission.
| |
- | //
| |
- | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
| |
- | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
| |
- | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
| |
- | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
| |
- | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
| |
- | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
| |
- | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
| |
- | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
| |
- | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
| |
- | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
| |
- | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
| |
- | //
| |
- | /**
| |
- | * All code is in an anonymous closure to keep the global namespace clean.
| |
- | *
| |
- | * @param {number=} overflow
| |
- | * @param {number=} startdenom
| |
- | */
| |
| | | |
- | // Patched by Seb so that seedrandom.js does not pollute the Math object.
| |
- | // My tests suggest that doing Math.trouble = 1 makes Math lookups about 5%
| |
- | // slower.
| |
| numeric.seedrandom = { pow:Math.pow, random:Math.random }; | | numeric.seedrandom = { pow:Math.pow, random:Math.random }; |
| | | |