-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstd-random.js
139 lines (126 loc) · 3.67 KB
/
std-random.js
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
/**
* Returns a random integer number
* with `min` inclusive and `max` exclusive [min, max - 1)
* @param {number} min
* @param {number} max
*/
function getRandomInt (min, max) {
min = Math.ceil(min)
max = Math.floor(max)
return Math.floor(Math.random() * (max - min)) + min
}
/**
* Returns if a number is integer
* @param {number} n Number to be tested
*/
function isInteger (n) {
return Number.isInteger(n)
}
/**
* StdRandom
* @classdesc JavaScript implementation of StdRandom.
* @see {@link https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/StdRandom.java.html}
*/
class StdRandom {
/**
* Uniform
*
* @todo Somehow I need to make this function to return
* real number values when [0.0, 1.0] are provided.
*
* @constructor
* @desc Returns a random real number uniformly in [0, 1).
* @returns {number} A random real number uniformly in [0, 1).
*//**
* Uniform
*
* @constructor
* @desc Returns a random integer uniformly in [0, n).
* @param {number} n Number of possible integers.
* @returns {number} A random integer uniformly between 0 (inclusive) and `n` (exclusive).
*//**
* Uniform
*
* @constructor
* @desc Returns a random integer uniformly in [lo, hi).
* @param {number} lo Minimum number.
* @param {number} hi Maximum number.
* @returns {number} A random integer uniformly in [lo, hi).
*//**
* Uniform
*
* @constructor
* @desc Returns a random real number uniformly in [lo, hi).
* @param {number} lo Minimum number.
* @param {number} hi Maximum number.
* @returns {number} A random real number uniformly in [lo, hi).
*/
static uniform (lo, hi) {
if (typeof lo === 'number' && typeof hi === 'number') {
if (isInteger(lo) && isInteger(hi)) {
return getRandomInt(lo, hi)
} else {
return Math.random() * (hi - lo) + lo
}
} else if (isInteger(lo)) {
return getRandomInt(0, lo)
}
/* NOTE: the book uses a signature like `uniform(0.0, 1.0)`
* to generate real or double numbers from [0, 1),
* in case you want create the same behaviour, then do
* `uniform()` and this will make the function to get to this
* returning value. In case you want to generate values from [0.0, 5.0)
* then I would recommend to do `uniform(0, 4.999)`.
*/
return Math.random()
}
/**
* Bernoulli
* @desc Returns a random boolean from a Bernoulli
* distribution with success probability 1/2.
* @param {number} [p=0.5] The probability of returning `true`.
* Defaults to `0.5`.
* @returns {boolean} `true` or `false` with probability of 1/2
*/
static bernoulli (p = 0.5) {
return StdRandom.uniform() < p
}
/**
* @todo implementation
*/
static gaussian () {
throw new SyntaxError('method gaussian not implemented')
}
/**
* Discrete
* @desc Returns a random integer from the specified discrete distribution.
* @param {[]} a Array of non-negative numbers that sum 1.
* @returns {number} A random integer from a discrete distribution:
* `i` with probability proportional to `frequencies[i]`.
*/
static discrete (a) {
const r = StdRandom.uniform()
let sum = 0
for (let i = 0; i < a.length; i++) {
sum += a[i]
if (sum >= r) return i
}
return -1
}
/**
* Shuffle
* @desc Rearranges the elements of the specified array in uniformly random order.
* @param {[]} a The array to shuffle
*/
static shuffle (a) {
const n = a.length
for (let i = 0; i < n; i++) {
// exchange a[i] with random element in a[i..n-1]
const r = i + StdRandom.uniform(n - i)
const temp = a[i]
a[i] = a[r]
a[r] = temp
}
}
}
module.exports = StdRandom