You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
1.5 KiB
35 lines
1.5 KiB
2 years ago
|
var $ = require('../internals/export');
|
||
|
var anObject = require('../internals/an-object');
|
||
|
var numberIsFinite = require('../internals/number-is-finite');
|
||
|
var createIteratorConstructor = require('../internals/create-iterator-constructor');
|
||
|
var InternalStateModule = require('../internals/internal-state');
|
||
|
|
||
|
var SEEDED_RANDOM = 'Seeded Random';
|
||
|
var SEEDED_RANDOM_GENERATOR = SEEDED_RANDOM + ' Generator';
|
||
|
var SEED_TYPE_ERROR = 'Math.seededPRNG() argument should have a "seed" field with a finite value.';
|
||
|
var setInternalState = InternalStateModule.set;
|
||
|
var getInternalState = InternalStateModule.getterFor(SEEDED_RANDOM_GENERATOR);
|
||
|
var $TypeError = TypeError;
|
||
|
|
||
|
var $SeededRandomGenerator = createIteratorConstructor(function SeededRandomGenerator(seed) {
|
||
|
setInternalState(this, {
|
||
|
type: SEEDED_RANDOM_GENERATOR,
|
||
|
seed: seed % 2147483647
|
||
|
});
|
||
|
}, SEEDED_RANDOM, function next() {
|
||
|
var state = getInternalState(this);
|
||
|
var seed = state.seed = (state.seed * 1103515245 + 12345) % 2147483647;
|
||
|
return { value: (seed & 1073741823) / 1073741823, done: false };
|
||
|
});
|
||
|
|
||
|
// `Math.seededPRNG` method
|
||
|
// https://github.com/tc39/proposal-seeded-random
|
||
|
// based on https://github.com/tc39/proposal-seeded-random/blob/78b8258835b57fc2100d076151ab506bc3202ae6/demo.html
|
||
|
$({ target: 'Math', stat: true, forced: true }, {
|
||
|
seededPRNG: function seededPRNG(it) {
|
||
|
var seed = anObject(it).seed;
|
||
|
if (!numberIsFinite(seed)) throw $TypeError(SEED_TYPE_ERROR);
|
||
|
return new $SeededRandomGenerator(seed);
|
||
|
}
|
||
|
});
|