forked from JaylyDev/ScriptAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.js
More file actions
118 lines (91 loc) · 3.69 KB
/
tests.js
File metadata and controls
118 lines (91 loc) · 3.69 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
// Script example for ScriptAPI
// Author: Jacob Rus <https://github.com/jrus>
// Project: https://github.com/JaylyDev/ScriptAPI
// https://github.com/JaylyDev/ScriptAPI
import random from "./index";
// set the PRNG’s seed, so that the precise sequence of numbers can be
// regenerated. When the class is first constructed, the PRNG is seeded
// with `+new Date`
random.seed(12345)
// choose a floating point number in the range [0, 1)
random.random()
// choose a floating point number in the range [1.5, 10)
random.uniform(1.5, 10)
// choose an integer N in the range 2 <= N < 50, by 2
random.randrange(2, 50, 2)
// or choose an integer N in the range 0 <= N < 45
random.randrange(45)
// choose an integer N in the range 5 <= N <= 18, endpoint included
random.randint(5, 18)
// randomly choose an element of an array
var array = 'abcdefg'.split('')
random.choice(array)
// choose 4 elements from the array, ordered, chosen without replacement
random.sample(array, 4)
// randomly shuffle the array, in place
random.shuffle(array)
// choose a random number from the standard normal distribution
random.gauss()
// choose a random number from the normal distribution with mean 5 and
// standard deviation 5
random.gauss(5, 5)
// choose from the triangular distribution on range [10, 20) with
// mode (peak) 18
random.triangular(10, 20, 18)
// choose from the triangular distribution on range [0, 1) with mode 0.5
random.triangular()
// choose from the log normal distribution. the log of this distribution
// is the normal distibution with mean 5 and standard deviation 5
random.lognormvariate(5, 5)
// choose from the Von Mises distribution, an analog of the normal distribution
// wrapped around a circle, with mean angle π, and concentration parameter π/2
random.vonmisesvariate(Math.PI, Math.PI/2)
// other distributions:
// - expovariate
// - gammavariate
// - betavariate
// - paretovariate
// - weibullvariate
// it’s possible to save and restore the state of the PRNG, allowing the same
// set of random numbers to be generated in the same order:
var some_state = random.getstate()
const a = random.random()
for (var index = 0; index < 1000; index++) {
random.random();
}
random.setstate(some_state)
random.random() == a
// * * * * * * * * * * * * *
// If the built-in PRNG doesn’t meet your needs, it is easy to
// override with your own PRNG. But this module also ships with
// a couple of alternatives.
const {BaseRandom, BuiltinRandom, HighQualityRandom} = random;
// First, `BuiltinRandom` generates random numbers approximately 10
// times as fast as `Random`. It calls the built-in `Math.random`
// function twice to construct a random number between 0 and 1
// with a full 52 bits of entropy. Unfortunately, typical JavaScript
// engines have PRNGs with rather poor performance on statistical
// tests of randomness, and this class also does not support setting
// a custom seed or saving/restoring the PRNG state.
var __random = new BuiltinRandom();
__random.random()
// The other PRNG provided has a much longer period and should pass
// more rigorous statistical tests, at the cost of running roughly 8–10
// times slower:
var __random = new HighQualityRandom();
__random.random()
// It is also quite straight-forward to implement a better custom PRNG:
class XKCDRandom extends BaseRandom {
// http://xkcd.com/221/
_randint32() { return 4 };
_seed(j) { return void 0;}; // ignore j
_getstate() { return void 0; };
_setstate() { return void 0; };
}
class DilbertRandom extends BaseRandom {
// http://dilbert.com/fast/2001-10-25/
_randint32() { return 9; };
_seed(j) { return void 0; }; // ignore j
_getstate() { return void 0; };
_setstate() { return void 0; };
}