-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
70 lines (54 loc) · 1.29 KB
/
index.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
import ParkMiller from 'park-miller';
import stringHash from '@sindresorhus/string-hash';
import color from 'color';
const MAX_INT32 = 2_147_483_647;
const GOLDEN_RATIO_CONJUGATE = 0.618_033_988_749_895;
export default class Randoma {
static seed() {
return Math.floor(Math.random() * MAX_INT32);
}
#random;
constructor({seed}) {
if (typeof seed === 'string') {
seed = stringHash(seed);
}
if (!Number.isInteger(seed)) {
throw new TypeError('Expected `seed` to be a `integer`');
}
this.#random = new ParkMiller(seed);
}
integer() {
return this.#random.integer();
}
integerInRange(minimum, maximum) {
return this.#random.integerInRange(minimum, maximum);
}
float() {
return this.#random.float();
}
floatInRange(minimum, maximum) {
return this.#random.floatInRange(minimum, maximum);
}
boolean() {
return this.#random.boolean();
}
arrayItem(array) {
return array[Math.floor(this.float() * array.length)];
}
date() {
return new Date(Date.now() * this.float());
}
dateInRange(startDate, endDate) {
return new Date(this.integerInRange(startDate.getTime(), endDate.getTime()));
}
color(saturation = 0.5) {
let hue = this.float();
hue += GOLDEN_RATIO_CONJUGATE;
hue %= 1;
return color({
h: hue * 360,
s: saturation * 100,
v: 95,
});
}
}