Node-Red configuration
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.

esnext.math.seeded-prng.js 1.6KB

123456789101112131415161718192021222324252627282930313233343536
  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. var SEEDED_RANDOM = 'Seeded Random';
  9. var SEEDED_RANDOM_GENERATOR = SEEDED_RANDOM + ' Generator';
  10. var SEED_TYPE_ERROR = 'Math.seededPRNG() argument should have a "seed" field with a finite value.';
  11. var setInternalState = InternalStateModule.set;
  12. var getInternalState = InternalStateModule.getterFor(SEEDED_RANDOM_GENERATOR);
  13. var $TypeError = TypeError;
  14. var $SeededRandomGenerator = createIteratorConstructor(function SeededRandomGenerator(seed) {
  15. setInternalState(this, {
  16. type: SEEDED_RANDOM_GENERATOR,
  17. seed: seed % 2147483647
  18. });
  19. }, SEEDED_RANDOM, function next() {
  20. var state = getInternalState(this);
  21. var seed = state.seed = (state.seed * 1103515245 + 12345) % 2147483647;
  22. return createIterResultObject((seed & 1073741823) / 1073741823, false);
  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 new $TypeError(SEED_TYPE_ERROR);
  31. return new $SeededRandomGenerator(seed);
  32. }
  33. });