-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrandom-tools.js
50 lines (42 loc) · 1.31 KB
/
random-tools.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
export function getUniform(max = 1, min = 0){
return min + Math.random() * (max - min);
}
//Box Muller
export function getNormal(mean = 0, standardDeviation = 1) {
const u1 = Math.random();
const u2 = Math.random();
return Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2) * standardDeviation + mean;
}
export function getInt(max = 1, min = 0){
return Math.floor(getUniform(max, min));
}
export function getBool(){
return getInt(2) === 0;
}
export function getOption(optionList){
const index = getInt(optionList.length);
return optionList[index];
}
const sliceHex = (arr, start, end) =>
Array.from(arr).slice(start, end).map(x => x.toString(16).toUpperCase().padStart(2, "0")).join("");
export function getGuid(){
const arr = new Uint8Array(16);
crypto.getRandomValues(arr);
return `${sliceHex(arr,0,4)}-${sliceHex(arr,4,6)}-${sliceHex(arr,6,8)}-${sliceHex(arr,8,10)}-${sliceHex(arr,10,16)}`;
}
/**
*
* @param {number} seed
* @param {number} start
* @param {number} end
* @description (Park Miller) seed should be greater than 0 and less than 0x7fffffff, output is 32-bit
*/
export function* getRandom(seed = 1, start = 0, end = 1){
const mod = 0x7fffffff;
let state = seed % mod;
const length = (end - start) / mod;
while(true){
state = (16807 * state) % mod;
yield (length * value) + start;
}
}