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