forked from JaylyDev/ScriptAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
653 lines (562 loc) · 20 KB
/
index.js
File metadata and controls
653 lines (562 loc) · 20 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
// Script example for ScriptAPI
// Author: Jacob Rus <https://github.com/jrus>
// Project: https://github.com/JaylyDev/ScriptAPI
// https://github.com/jrus/random-js
// Generated by CoffeeScript 2.7.0
/*
A fairly direct port of the Python `random` module to JavaScript.
https://github.com/jrus/random-js/blob/master/license.mdown
## MIT License:
Copyright (c) 2012 Jacob Rus,
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* * *
Much of this library is straight-forward ports from the Python Random module:
## PSF LICENSE AGREEMENT FOR PYTHON 3.2.2
1. This LICENSE AGREEMENT is between the Python Software Foundation
(“PSF”), and the Individual or Organization (“Licensee”) accessing
and otherwise using Python 3.2.2 software in source or binary form
and its associated documentation.
2. Subject to the terms and conditions of this License Agreement, PSF
hereby grants Licensee a nonexclusive, royalty-free, world-wide
license to reproduce, analyze, test, perform and/or display publicly,
prepare derivative works, distribute, and otherwise use Python 3.2.2
alone or in any derivative version, provided, however, that PSF’s
License Agreement and PSF’s notice of copyright, i.e., “Copyright ©
2001-2012 Python Software Foundation; All Rights Reserved” are
retained in Python 3.2.2 alone or in any derivative version prepared
by Licensee.
3. In the event Licensee prepares a derivative work that is based on
or incorporates Python 3.2.2 or any part thereof, and wants to make
the derivative work available to others as provided herein, then
Licensee hereby agrees to include in any such work a brief summary
of the changes made to Python 3.2.2.
4. PSF is making Python 3.2.2 available to Licensee on an “AS IS”
basis. PSF MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR
IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, PSF MAKES NO AND
DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR
FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF PYTHON 3.2.2
WILL NOT INFRINGE ANY THIRD PARTY RIGHTS.
5. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON
3.2.2 FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS
AS A RESULT OF MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON
3.2.2, OR ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF THE
POSSIBILITY THEREOF.
6. This License Agreement will automatically terminate upon a material
breach of its terms and conditions.
7. Nothing in this License Agreement shall be deemed to create any
relationship of agency, partnership, or joint venture between PSF
and Licensee. This License Agreement does not grant permission to
use PSF trademarks or trade name in a trademark sense to endorse or
promote products or services of Licensee, or any third party.
8. By copying, installing or otherwise using Python 3.2.2, Licensee
agrees to be bound by the terms and conditions of this License
Agreement.
*/
var BaseRandom, BuiltinRandom, HighQualityRandom, LOG2E, NotImplementedError, POW_32, POW_NEG_32, Random, acos, bind, cos, exp, exports, extend, floor, lg, log, mod, pow, sqrt,
indexOf = [].indexOf,
boundMethodCheck = function(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new Error('Bound instance method accessed before binding'); } };
({log, sqrt, cos, acos, floor, pow, LOG2E, exp} = Math);
POW_32 = pow(2, 32);
POW_NEG_32 = pow(2, -32);
lg = function(x) {
// The log base 2, rounded down to the integer below
return (LOG2E * log(x + 1e-10)) >> 0;
};
mod = function(x, y) {
var jsmod;
if (!((jsmod = x % y) && (x > 0 !== y > 0))) {
return jsmod;
} else {
return jsmod + y;
}
};
extend = function(target, ...sources) {
var l, len, method, name, obj;
for (l = 0, len = sources.length; l < len; l++) {
obj = sources[l];
for (name in obj) {
method = obj[name];
target[name] = method;
}
}
return target;
};
bind = function(fn, obj) {
return function() {
return fn.apply(obj, arguments);
};
};
NotImplementedError = class NotImplementedError extends Error {};
BaseRandom = (function() {
var E, LOG4, POW_NEG_26, POW_NEG_27, SG_MAGICCONST, TAU, _bits;
class BaseRandom {
//# Override these first four methods in a custom Random class.
_randint32() {
// Override this method to generate a pseudorandom number
throw new NotImplementedError();
}
_getstate() {
// Override this method to fetch the internal PRNG state. Should
// return an Array.
throw new NotImplementedError();
}
// @ts-ignore
_setstate(state) {
// Override this method to set the internal PRNG state from the
// argument `state`, an Array.
throw new NotImplementedError();
}
// @ts-ignore
_seed(...args) {
// Override this method to seed the PRNG
throw new NotImplementedError();
}
//# Generally no need to override the methods below in a custom class.
//# (Under some circumstances it might make sense to implement a custom
//# version of the `random` method or add to the constructor.)
constructor() {
this.seed = this.seed.bind(this);
this.random = this.random.bind(this);
this.setstate = this.setstate.bind(this);
this.getstate = this.getstate.bind(this);
this.uniform = this.uniform.bind(this);
this.randrange = this.randrange.bind(this);
this.randint = this.randint.bind(this);
this.choice = this.choice.bind(this);
this.sample = this.sample.bind(this);
this.shuffle = this.shuffle.bind(this);
this.gauss = this.gauss.bind(this);
this.triangular = this.triangular.bind(this);
this.lognormvariate = this.lognormvariate.bind(this);
this.expovariate = this.expovariate.bind(this);
this.vonmisesvariate = this.vonmisesvariate.bind(this);
this.gammavariate = this.gammavariate.bind(this);
this.betavariate = this.betavariate.bind(this);
this.paretovariate = this.paretovariate.bind(this);
this.weibullvariate = this.weibullvariate.bind(this);
// bind `normalvariate` (def. below as a `gauss` alias) to the instance
this.normalvariate = bind(this.normalvariate, this);
// By default, just seed the PRNG with the date. Some PRNGs
// can take longer and more complex seeds.
this._next_gauss = null;
this.seed(+new Date());
}
seed(...args) {
// Seed the PRNG.
return this._seed(...args);
}
random() {
var high_bits, low_bits;
// Return a random float in the range [0, 1), with a full 53
// bits of entropy.
// @ts-ignore
low_bits = this._randint32() >>> 6;
// @ts-ignore
high_bits = this._randint32() >>> 5;
return (high_bits + low_bits * POW_NEG_26) * POW_NEG_27;
}
setstate([_next_gauss, ...state]) {
this._next_gauss = _next_gauss;
// Set the state of the PRNG. Should accept the output of `@getstate`
// as its only argument.
return this._setstate(state);
}
getstate() {
// Get the internal state of the PRNG. Returns an array of state
// information suitable for passing into `@setstate`.
// @ts-ignore
return [this._next_gauss, ...this._getstate()];
}
_randbelow(n) {
var bits, r;
// Return a random int in the range [0,n).
// If n > 2^32, then use floating point math
if (n <= 0x100000000) {
bits = _bits[n] || (_bits[n] = (lg(n - 1)) + 1); // memoize values for `bits`
while (true) {
// @ts-ignore
r = this._randint32() >>> (32 - bits);
if (r < 0) {
r += POW_32;
}
if (r < n) {
break;
}
}
return r;
} else {
return floor(this.random() * n);
}
}
uniform(a, b) {
// Return a random floating point number N such that a <= N <= b for
// a <= b and b <= N <= a for b < a.
return a + this.random() * (b - a);
}
randrange(start, stop, step) {
// Return a random integer N in range `[start...stop] by step`
if (stop == null) {
return this._randbelow(start);
} else if (!step) {
return start + this._randbelow(stop - start);
} else {
return start + step * this._randbelow(floor((stop - start) / step));
}
}
randint(a, b) {
// Return a random integer N in range `[a..b]`
return a + this._randbelow(1 + b - a);
}
choice(seq) {
// Return a random element from the non-empty sequence `seq`.
return seq[this._randbelow(seq.length)];
}
sample(population, k = 1) {
// @ts-ignore
var i, j, l, m, n, pool, ref, ref1, ref2, ref3, results, results1, selected, val;
// Return a `k` length list of unique elements chosen from the
// `population` sequence. Used for random sampling without replacement.
n = population.length;
if (k > n) {
throw new Error("can't take a sample bigger than the population");
}
if (k * 3 > n) { // for large samples, copy the
pool = [...population]; // population as a new array
results = [];
for (i = l = ref = n, ref1 = n - k; l > ref1; i = l += -1) {
j = this._randbelow(i);
val = pool[j];
pool[j] = pool[i - 1];
results.push(val); // for small samples, treat an Array
}
return results;
} else {
selected = []; // as a set to keep track of selection
results1 = [];
for (i = m = 0, ref2 = k; m < ref2; i = m += 1) {
while (true) {
if (ref3 = (j = this._randbelow(n)), indexOf.call(selected, ref3) < 0) {
break;
}
}
selected.push(j);
results1.push(population[j]);
}
return results1;
}
}
shuffle(x) {
// @ts-ignore
var i, j, l, ref, tmp;
// Shuffle the sequence x in place.
for (i = l = ref = x.length - 1; l >= 1; i = l += -1) {
j = this._randbelow(i + 1);
tmp = x[i];
x[i] = x[j];
x[j] = tmp; // swap x[i], x[j]
}
return x;
}
gauss(mu = 0, sigma = 1) {
var s, u, v, w, z;
// Gaussian distribution. `mu` is the mean, and `sigma` is the standard
// deviation. Notes:
// * uses the "polar method"
// * we generate pairs; keep one in a cache for next time
if ((z = this._next_gauss) != null) {
this._next_gauss = null;
} else {
while (!(s && s < 1)) {
u = 2 * this.random() - 1;
v = 2 * this.random() - 1;
s = u * u + v * v;
}
w = sqrt(-2 * (log(s)) / s);
z = u * w;
this._next_gauss = v * w;
}
return mu + z * sigma;
}
triangular(low, high, mode) {
var c, u;
// Triangular distribution. See wikipedia
if (low == null) {
high = 1;
low = 0;
} else if (high == null) {
high = low;
low = 0;
}
if (mode == null) {
c = 0.5;
} else {
c = (mode - low) / (high - low);
}
u = this.random();
if (u <= c) {
return low + (high - low) * sqrt(u * c);
} else {
return high - (high - low) * sqrt((1 - u) * (1 - c));
}
}
lognormvariate(mu, sigma) {
// Log normal distribution.
return exp(this.normalvariate(mu, sigma));
}
expovariate(lambda) {
// Exponential distribution.
// `lambda` is 1.0 divided by the desired mean. It should be nonzero.
// Returned values range from 0 to positive infinity if lambda is positive,
// and from negative infinity to 0 if lambda is negative.
// we use 1 - random() instead of random() to preclude the
// possibility of taking the log of zero.
return (-log(1 - this.random())) / lambda;
}
vonmisesvariate(mu, kappa) {
var a, b, c, f, r, rand, u1, u2, u3, z;
// Circular data distribution.
// mu is the mean angle, expressed in radians between 0 and 2*pi, and
// kappa is the concentration parameter, which must be greater than or
// equal to zero. If kappa is equal to zero, this distribution reduces
// to a uniform random angle over the range 0 to 2*pi.
// Based upon an algorithm published in: Fisher, N.I.,
// "Statistical Analysis of Circular Data", Cambridge
// University Press, 1993.
rand = this.random;
if (kappa <= 1e-6) {
return TAU * rand();
}
a = 1 + sqrt(1 + 4 * kappa * kappa);
b = (1 - sqrt(2)) * a / 2 / kappa;
r = (1 + b * b) / 2 / b;
while (true) {
u1 = rand();
z = cos(TAU * u1 / 2);
f = (1 + r * z) / (r + z);
c = kappa * (r - f);
u2 = rand();
if (u2 < c * (2 - c) || u2 <= c * exp(1 - c)) {
break;
}
}
u3 = rand();
return (mod(mu, TAU)) + (u3 > 0.5 ? acos(f) : -acos(f));
}
gammavariate(alpha, beta) {
var ainv, b, bbb, ccc, p, r, rand, u, u1, u2, v, x, z;
// Gamma distribution. Not the gamma function!
// Conditions on the parameters are alpha > 0 and beta > 0.
// The probability distribution function is:
// x ** (alpha - 1) * exp( -x / beta)
// pdf(x) = ----------------------------------
// gamma(alpha) * beta ** alpha
// alpha > 0, beta > 0, mean is alpha * beta, variance is alpha * beta**2
// Warning: a few older sources define the gamma distribution in terms
// of alpha > -1
rand = this.random;
if (alpha > 1) {
// Uses R.C.H. Cheng, "The generation of Gamma
// variables with non-integral shape parameters",
// Applied Statistics, (1977), 26, No. 1, p71-74
ainv = sqrt(2 * alpha - 1);
bbb = alpha - LOG4;
ccc = alpha + ainv;
while (true) {
u1 = rand();
if (!((1e-7 < u1 && u1 < 1 - 1e-7))) {
continue;
}
u2 = 1 - rand();
v = (log(u1 / (1 - u1))) / ainv;
x = alpha * exp(v);
z = u1 * u1 * u2;
r = bbb + ccc * v - x;
if (r + SG_MAGICCONST - 4.5 * z >= 0.0 || r >= log(z)) {
break;
}
}
return beta * x;
} else if (alpha === 1) {
while (true) {
// expovariate(1)
u = rand();
if (u > 1e-7) {
break;
}
}
return -beta * log(u); // alpha is between 0 and 1 (exclusive)
} else {
while (true) {
// Uses ALGORITHM GS of Statistical Computing - Kennedy & Gentle
u1 = rand();
b = (E + alpha) / E;
p = b * u1;
u2 = rand();
if (p > 1) {
x = -log((b - p) / alpha);
if (u2 <= pow(x, alpha - 1)) {
break;
}
} else {
x = pow(p, 1 / alpha);
if (u2 <= exp(-x)) {
break;
}
}
}
return beta * x;
}
}
betavariate(alpha, beta) {
var y;
// Beta distribution.
// Conditions on the parameters are alpha > 0 and beta > 0.
// Returned values range between 0 and 1.
// This version due to Janne Sinkkonen, and matches all the std
// texts (e.g., Knuth Vol 2 Ed 3 pg 134 "the beta distribution").
y = this.gammavariate(alpha, 1);
if (y === 0) {
return 0;
} else {
return y / (y + this.gammavariate(beta, 1));
}
}
paretovariate(alpha) {
var u;
// Pareto distribution. alpha is the shape parameter.
u = 1 - this.random();
return 1 / (pow(u, 1 / alpha)); // Jain, pg. 495
}
weibullvariate(alpha, beta) {
var u;
// Weibull distribution.
// alpha is the scale parameter and beta is the shape parameter.
u = 1 - this.random();
return alpha * (pow(-log(u, 1 / beta))); // Jain, pg. 499; bug fix by Bill Arms
}
};
POW_NEG_26 = pow(2, -26);
POW_NEG_27 = pow(2, -27);
_bits = {};
BaseRandom.prototype.normalvariate = BaseRandom.prototype.gauss; // Alias for the `gauss` function
TAU = 2 * Math.PI;
LOG4 = log(4);
SG_MAGICCONST = 1 + log(4.5);
E = {Math};
return BaseRandom;
}).call(this);
Random = class Random extends BaseRandom {
// Use a Multiply With Carry PRNG, with an XOR-shift successor
// Both from Numerical Recipes, 3rd Edition [H1, G1]
_randint32() {
var z;
this.x = 62904 * (this.x & 0xffff) + (this.x >>> 16);
this.y = 41874 * (this.y & 0xffff) + (this.y >>> 16);
z = (this.x << 16) + this.y;
z ^= z >>> 13;
z ^= z << 17;
z ^= z >>> 5;
return z;
}
_seed(j) {
// these two numbers were arbitrarily chosen
this.x = 3395989511 ^ j;
return this.y = 1716319410 ^ j;
}
_getstate() {
return [this.x, this.y];
}
_setstate([x1, y1]) {
this.x = x1;
this.y = y1;
}
};
HighQualityRandom = class HighQualityRandom extends BaseRandom {
// From Numerical Recipes, 3rd Edition
_randint32() {
var v, x, y;
x = this.u = this.u * 2891336453 + 1640531513;
v = this.v;
v ^= v >>> 13;
v ^= v << 17;
v ^= v >>> 5;
this.v = v;
y = this.w1 = 33378 * (this.w1 & 0xffff) + (this.w1 >>> 16);
this.w2 = 57225 * (this.w2 & 0xffff) + (this.w2 >>> 16);
x ^= x << 9;
x ^= x >>> 17;
x ^= x << 6;
y ^= y << 17;
y ^= y >>> 15;
y ^= y << 5;
return (x + v) ^ (y + this.w2);
}
_seed(j) {
this.w1 = 521288629;
this.w2 = 362436069;
return this.v = this.u = j ^ 2244614371;
}
_getstate() {
return [this.u, this.v, this.w1, this.w2];
}
_setstate([u4, v1, w1, w2]) {
this.u = u4;
this.v = v1;
this.w1 = w1;
this.w2 = w2;
}
};
BuiltinRandom = (function() {
var _lowbits, _rand;
class BuiltinRandom extends BaseRandom {
constructor() {
super(...arguments);
// Use the built-in PRNG. Note that with the built-in
// PRNG, which is implementation dependant, there is no
// way to set the seed or save/restore state.
this._seed = this._seed.bind(this);
}
// @ts-ignore
_seed(j) { // ignore seed
boundMethodCheck(this, BuiltinRandom);
}
_randint32() {
return (_rand() * POW_32) | 0;
}
};
// Test to see if our JavaScript engine creates random numbers
// with more than 32 bits of entropy. If so, just use it directly.
// Otherwise, combine two calls to `random` into each invocation.
_rand = Math.random;
_lowbits = function() {
return (_rand() * pow(2, 64)) | 0; // `| 0` will chop out bits > 32
};
if (_lowbits() | _lowbits() | _lowbits()) { // ~1e-18 chance of false negative
// @ts-ignore
BuiltinRandom.prototype.random = _rand;
} else {
// @ts-ignore
BuiltinRandom.prototype.random = function() {
return _rand() * POW_NEG_32 + _rand();
};
}
return BuiltinRandom;
}).call(this);
exports = {};
extend(exports, {NotImplementedError, BaseRandom, Random, HighQualityRandom, BuiltinRandom}, new Random());
export default exports;