123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- goog.setTestOnly('goog.testing.PseudoRandom');
- goog.provide('goog.testing.PseudoRandom');
- goog.require('goog.Disposable');
- goog.testing.PseudoRandom = function(opt_seed, opt_install) {
- goog.Disposable.call(this);
- if (!goog.isDef(opt_seed)) {
- opt_seed = goog.testing.PseudoRandom.seedUniquifier_++ + goog.now();
- }
- this.seed(opt_seed);
- if (opt_install) {
- this.install();
- }
- };
- goog.inherits(goog.testing.PseudoRandom, goog.Disposable);
- goog.testing.PseudoRandom.seedUniquifier_ = 0;
- goog.testing.PseudoRandom.A = 48271;
- goog.testing.PseudoRandom.M = 2147483647;
- goog.testing.PseudoRandom.Q = 44488;
- goog.testing.PseudoRandom.R = 3399;
- goog.testing.PseudoRandom.ONE_OVER_M_MINUS_ONE =
- 1.0 / (goog.testing.PseudoRandom.M - 1);
- goog.testing.PseudoRandom.prototype.seed_ = 1;
- goog.testing.PseudoRandom.prototype.installed_;
- goog.testing.PseudoRandom.prototype.mathRandom_;
- goog.testing.PseudoRandom.prototype.install = function() {
- if (!this.installed_) {
- this.mathRandom_ = Math.random;
- Math.random = goog.bind(this.random, this);
- this.installed_ = true;
- }
- };
- goog.testing.PseudoRandom.prototype.disposeInternal = function() {
- goog.testing.PseudoRandom.superClass_.disposeInternal.call(this);
- this.uninstall();
- };
- goog.testing.PseudoRandom.prototype.uninstall = function() {
- if (this.installed_) {
- Math.random = this.mathRandom_;
- this.installed_ = false;
- }
- };
- goog.testing.PseudoRandom.prototype.seed = function(opt_seed) {
- this.seed_ = opt_seed % (goog.testing.PseudoRandom.M - 1);
- if (this.seed_ <= 0) {
- this.seed_ += goog.testing.PseudoRandom.M - 1;
- }
- };
- goog.testing.PseudoRandom.prototype.random = function() {
- var hi = Math.floor(this.seed_ / goog.testing.PseudoRandom.Q);
- var lo = this.seed_ % goog.testing.PseudoRandom.Q;
- var test =
- goog.testing.PseudoRandom.A * lo - goog.testing.PseudoRandom.R * hi;
- if (test > 0) {
- this.seed_ = test;
- } else {
- this.seed_ = test + goog.testing.PseudoRandom.M;
- }
- return (this.seed_ - 1) * goog.testing.PseudoRandom.ONE_OVER_M_MINUS_ONE;
- };
|