[6a3a178] | 1 | var $ = require('../internals/export');
|
---|
| 2 | var anObject = require('../internals/an-object');
|
---|
| 3 | var numberIsFinite = require('../internals/number-is-finite');
|
---|
| 4 | var createIteratorConstructor = require('../internals/create-iterator-constructor');
|
---|
| 5 | var InternalStateModule = require('../internals/internal-state');
|
---|
| 6 |
|
---|
| 7 | var SEEDED_RANDOM = 'Seeded Random';
|
---|
| 8 | var SEEDED_RANDOM_GENERATOR = SEEDED_RANDOM + ' Generator';
|
---|
| 9 | var setInternalState = InternalStateModule.set;
|
---|
| 10 | var getInternalState = InternalStateModule.getterFor(SEEDED_RANDOM_GENERATOR);
|
---|
| 11 | var SEED_TYPE_ERROR = 'Math.seededPRNG() argument should have a "seed" field with a finite value.';
|
---|
| 12 |
|
---|
| 13 | var $SeededRandomGenerator = createIteratorConstructor(function SeededRandomGenerator(seed) {
|
---|
| 14 | setInternalState(this, {
|
---|
| 15 | type: SEEDED_RANDOM_GENERATOR,
|
---|
| 16 | seed: seed % 2147483647
|
---|
| 17 | });
|
---|
| 18 | }, SEEDED_RANDOM, function next() {
|
---|
| 19 | var state = getInternalState(this);
|
---|
| 20 | var seed = state.seed = (state.seed * 1103515245 + 12345) % 2147483647;
|
---|
| 21 | return { value: (seed & 1073741823) / 1073741823, done: false };
|
---|
| 22 | });
|
---|
| 23 |
|
---|
| 24 | // `Math.seededPRNG` method
|
---|
| 25 | // https://github.com/tc39/proposal-seeded-random
|
---|
| 26 | // based on https://github.com/tc39/proposal-seeded-random/blob/78b8258835b57fc2100d076151ab506bc3202ae6/demo.html
|
---|
| 27 | $({ target: 'Math', stat: true, forced: true }, {
|
---|
| 28 | seededPRNG: function seededPRNG(it) {
|
---|
| 29 | var seed = anObject(it).seed;
|
---|
| 30 | if (!numberIsFinite(seed)) throw TypeError(SEED_TYPE_ERROR);
|
---|
| 31 | return new $SeededRandomGenerator(seed);
|
---|
| 32 | }
|
---|
| 33 | });
|
---|