-
Notifications
You must be signed in to change notification settings - Fork 7
/
Random.js
52 lines (47 loc) · 1.17 KB
/
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
//@ts-check
'use strict';
/* jshint esversion:6 */
/**
* Creates a pseudo-random value generator. The seed must be an integer.
*
* Uses an optimized version of the Park-Miller PRNG.
* http://www.firstpr.com.au/dsp/rand31/
*
* https://gist.github.com/blixt/f17b47c62508be59987b
*/
export default class Random {
/**
* @param {number} seed
*/
constructor(seed) {
this.seed_ = seed % 2147483647;
if ( this.seed_ <= 0 ) {
this.seed_ += 2147483646;
}
}
/**
* Returns a pseudo-random value between 1 and 2^32 - 2.
* @return {number}
*/
next() {
return this.seed_ = this.seed_ * 16807 % 2147483647;
}
/**
* Returns a pseudo-random floating point number in range [0, 1].
* @return {number}
*/
nextFloat() {
// We know that result of next() will be 1 to 2147483646 (inclusive).
return (this.next() - 1) / 2147483646;
}
/**
* Returns a random integer between min (inclusive) and max (inclusive)
* Using Math.round() will give you a non-uniform distribution!
* @param {number} min
* @param {number} max
* @return {number}
*/
nextInt(min, max) {
return Math.floor(this.nextFloat() * (max - min + 1)) + min;
}
}