Files
SMANAGE/TempLate/MSOC/Python/Lib/ScriptLib/random.js
T
2026-06-16 11:55:14 +08:00

127 lines
8.7 KiB
JavaScript

var MersenneTwister = function(a) {
a == null && (a = new Date()
.getTime()), this.N = 624, this.M = 397, this.MATRIX_A = 2567483615, this.UPPER_MASK = 2147483648, this.LOWER_MASK = 2147483647, this.mt = Array(this.N), this.mti = this.N + 1, this.init_genrand(a)
};
MersenneTwister.prototype.init_genrand = function(a) {
for (this.mt[0] = a >>> 0, this.mti = 1; this.mti < this.N; this.mti++) {
var a = this.mt[this.mti - 1] ^ this.mt[this.mti - 1] >>> 30;
this.mt[this.mti] = (1812433253 * ((4294901760 & a) >>> 16) << 16) + 1812433253 * (65535 & a) + this.mti, this.mt[this.mti] >>>= 0
}
}, MersenneTwister.prototype.init_by_array = function(a, b) {
var d, e, f;
for (this.init_genrand(19650218), d = 1, e = 0, f = this.N > b ? this.N : b; f; f--) {
var g = this.mt[d - 1] ^ this.mt[d - 1] >>> 30;
this.mt[d] = (this.mt[d] ^ (1664525 * ((4294901760 & g) >>> 16) << 16) + 1664525 * (65535 & g)) + a[e] + e, this.mt[d] >>>= 0, d++, e++, d >= this.N && (this.mt[0] = this.mt[this.N - 1], d = 1), e >= b && (e = 0)
}
for (f = this.N - 1; f; f--) {
var g = this.mt[d - 1] ^ this.mt[d - 1] >>> 30;
this.mt[d] = (this.mt[d] ^ (1566083941 * ((4294901760 & g) >>> 16) << 16) + 1566083941 * (65535 & g)) - d, this.mt[d] >>>= 0, d++, d >= this.N && (this.mt[0] = this.mt[this.N - 1], d = 1)
}
this.mt[0] = 2147483648
}, MersenneTwister.prototype.genrand_int32 = function() {
var a, b = [0, this.MATRIX_A];
if (this.mti >= this.N) {
var d;
for (this.mti == this.N + 1 && this.init_genrand(5489), d = 0; d < this.N - this.M; d++) a = this.mt[d] & this.UPPER_MASK | this.mt[d + 1] & this.LOWER_MASK, this.mt[d] = this.mt[d + this.M] ^ a >>> 1 ^ b[1 & a];
for (; d < this.N - 1; d++) a = this.mt[d] & this.UPPER_MASK | this.mt[d + 1] & this.LOWER_MASK, this.mt[d] = this.mt[d + (this.M - this.N)] ^ a >>> 1 ^ b[1 & a];
a = this.mt[this.N - 1] & this.UPPER_MASK | this.mt[0] & this.LOWER_MASK, this.mt[this.N - 1] = this.mt[this.M - 1] ^ a >>> 1 ^ b[1 & a], this.mti = 0
}
return a = this.mt[this.mti++], a ^= a >>> 11, a ^= 2636928640 & a << 7, a ^= 4022730752 & a << 15, a ^= a >>> 18, a >>> 0
}, MersenneTwister.prototype.genrand_int31 = function() {
return this.genrand_int32() >>> 1
}, MersenneTwister.prototype.genrand_real1 = function() {
return this.genrand_int32() * (1 / 4294967295)
}, MersenneTwister.prototype.random = function() {
return this.genrand_int32() * (1 / 4294967296)
}, MersenneTwister.prototype.genrand_real3 = function() {
return (this.genrand_int32() + .5) * (1 / 4294967296)
}, MersenneTwister.prototype.genrand_res53 = function() {
var d = this.genrand_int32() >>> 5,
a = this.genrand_int32() >>> 6;
return (67108864 * d + a) * (1 / 9007199254740992)
};
var $builtinmodule = function() {
var a = Math.log,
b = Math.sqrt,
d = {},
e = new MersenneTwister,
f = void 0;
d.seed = new Sk.builtin.func(function(a) {
return Sk.builtin.pyCheckArgsLen("seed", arguments.length, 0, 1), a = Sk.builtin.asnum$(a), e = 0 < arguments.length ? new MersenneTwister(a) : new MersenneTwister, Sk.builtin.none.none$
}), d.random = new Sk.builtin.func(function() {
return Sk.builtin.pyCheckArgsLen("random", arguments.length, 0, 0), new Sk.builtin.float_(e.genrand_res53())
});
var g = function(a) {
return 0 | a
},
h = function(a, b, d) {
var f, h, i;
if (!Sk.builtin.checkInt(a)) throw new Sk.builtin.ValueError("non-integer first argument for randrange()");
if (void 0 === b) return i = g(e.genrand_res53() * a), new Sk.builtin.int_(i);
if (!Sk.builtin.checkInt(b)) throw new Sk.builtin.ValueError("non-integer stop for randrange()");
if (void 0 === d && (d = 1), f = b - a, 1 == d && 0 < f) return i = a + g(e.genrand_res53() * f), new Sk.builtin.int_(i);
if (1 == d) throw new Sk.builtin.ValueError("empty range for randrange() (" + a + ", " + b + ", " + f + ")");
if (!Sk.builtin.checkInt(d)) throw new Sk.builtin.ValueError("non-integer step for randrange()");
if (0 < d) h = g((f + d - 1) / d);
else if (0 > d) h = g((f + d + 1) / d);
else throw new Sk.builtin.ValueError("zero step for randrange()");
if (0 >= h) throw new Sk.builtin.ValueError("empty range for randrange()");
return i = a + d * g(e.genrand_res53() * h), new Sk.builtin.int_(i)
};
d.randint = new Sk.builtin.func(function(d, e) {
return Sk.builtin.pyCheckArgsLen("randint", arguments.length, 2, 2), d = Sk.builtin.asnum$(d), e = Sk.builtin.asnum$(e), h(d, e + 1)
}), d.randrange = new Sk.builtin.func(function(a, b, d) {
return Sk.builtin.pyCheckArgsLen("randrange", arguments.length, 1, 3), a = Sk.builtin.asnum$(a), b = Sk.builtin.asnum$(b), d = Sk.builtin.asnum$(d), h(a, b, d)
}), d.uniform = new Sk.builtin.func(function(d, f) {
Sk.builtin.pyCheckArgsLen("uniform", arguments.length, 2, 2), d = Sk.builtin.asnum$(d), f = Sk.builtin.asnum$(f);
var g = e.genrand_res53();
return c = d + g * (f - d), new Sk.builtin.float_(c)
}), d.triangular = new Sk.builtin.func(function(a, d, f) {
Sk.builtin.pyCheckArgsLen("triangular", arguments.length, 2, 3), Sk.builtin.pyCheckType("low", "number", Sk.builtin.checkNumber(a)), Sk.builtin.pyCheckType("high", "number", Sk.builtin.checkNumber(d));
var g, h, i;
return a = Sk.builtin.asnum$(a), d = Sk.builtin.asnum$(d), a > d && (i = a, a = d, d = i), void 0 === f || f === Sk.builtin.none.none$ ? f = (d - a) / 2 : (Sk.builtin.pyCheckType("mode", "number", Sk.builtin.checkNumber(f)), f = Sk.builtin.asnum$(f)), g = e.genrand_res53(), h = g < (f - a) / (d - a) ? a + b(g * (d - a) * (f - a)) : d - b((1 - g) * (d - a) * (d - f)), new Sk.builtin.float_(h)
});
var i = function(d, g) {
var k, l, m, n, o, h = Math.sin,
i = Math.cos,
j = Math.PI;
return void 0 === f ? (k = e.genrand_res53(), l = e.genrand_res53(), m = b(-2 * a(k)), n = 2 * j * l, o = m * i(n), f = m * h(n)) : (o = f, f = void 0), d + g * o
};
return d.gauss = new Sk.builtin.func(function(a, b) {
return Sk.builtin.pyCheckArgsLen("gauss", arguments.length, 2, 2), Sk.builtin.pyCheckType("mu", "number", Sk.builtin.checkNumber(a)), Sk.builtin.pyCheckType("sigma", "number", Sk.builtin.checkNumber(b)), a = Sk.builtin.asnum$(a), b = Sk.builtin.asnum$(b), new Sk.builtin.float_(i(a, b))
}), d.normalvariate = d.gauss, d.lognormvariate = new Sk.builtin.func(function(a, b) {
var d = Math.exp;
return Sk.builtin.pyCheckArgsLen("lognormvariate", arguments.length, 2, 2), Sk.builtin.pyCheckType("mu", "number", Sk.builtin.checkNumber(a)), Sk.builtin.pyCheckType("sigma", "number", Sk.builtin.checkNumber(b)), a = Sk.builtin.asnum$(a), b = Sk.builtin.asnum$(b), new Sk.builtin.float_(d(i(a, b)))
}), d.expovariate = new Sk.builtin.func(function(b) {
Sk.builtin.pyCheckArgsLen("expovariate", arguments.length, 1, 1), Sk.builtin.pyCheckType("lambd", "number", Sk.builtin.checkNumber(b)), b = Sk.builtin.asnum$(b);
var d = e.genrand_res53();
return new Sk.builtin.float_(-a(d) / b)
}), d.choice = new Sk.builtin.func(function(a) {
if (Sk.builtin.pyCheckArgsLen("choice", arguments.length, 1, 1), Sk.builtin.pyCheckType("seq", "sequence", Sk.builtin.checkSequence(a)), void 0 !== a.sq$length) {
var b = new Sk.builtin.int_(g(e.genrand_res53() * a.sq$length()));
return a.mp$subscript(b)
}
throw new Sk.builtin.TypeError("object has no length")
}), d.shuffle = new Sk.builtin.func(function(a) {
if (Sk.builtin.pyCheckArgsLen("shuffle", arguments.length, 1, 1), Sk.builtin.pyCheckType("x", "sequence", Sk.builtin.checkSequence(a)), a.constructor === Sk.builtin.list) {
const h = a.v;
for (var b = h.length - 1; 0 < b; b -= 1) {
var d = g(e.genrand_res53() * (b + 1)),
f = h[d];
h[d] = h[b], h[b] = f
}
} else if (void 0 === a.sq$length) throw new Sk.builtin.TypeError("object has no length");
else if (void 0 !== a.mp$ass_subscript)
for (var d, b = a.sq$length() - 1; 0 < b; b -= 1) {
d = new Sk.builtin.int_(g(e.genrand_res53() * (b + 1))), b = new Sk.builtin.int_(b);
var f = a.mp$subscript(d);
a.mp$ass_subscript(d, a.mp$subscript(b)), a.mp$ass_subscript(b, f)
} else throw new Sk.builtin.TypeError("object is immutable");
return Sk.builtin.none.none$
}), d.sample = new Sk.builtin.func(function(a, b) {
var f, g, h, l, m, d = Math.floor;
for (Sk.builtin.pyCheckArgsLen("sample", arguments.length, 2, 2), Sk.builtin.pyCheckType("population", "iterable", Sk.builtin.checkIterable(a)), Sk.builtin.pyCheckType("k", "integer", Sk.builtin.checkInt(b)), b = Sk.builtin.asnum$(b), m = [], h = Sk.abstr.iter(a), (f = 0, l = h.tp$iternext()); void 0 !== l; f++, l = h.tp$iternext()) g = d(e.genrand_res53() * (f + 1)), f < b ? (g < f && (m[f] = m[g]), m[g] = l) : g < b && (m[g] = l);
if (f < b) throw new Sk.builtin.ValueError("sample larger than population");
return new Sk.builtin.list(m)
}), d
};