-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.js
31 lines (24 loc) · 875 Bytes
/
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
export function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = getRandomInt(i + 1);
[array[i], array[j]] = [array[j], array[i]];
}
}
export function getRandomInt(max) {
const array = new Uint32Array(1);
window.crypto.getRandomValues(array);
return Math.floor(array[0] / 4294967296 * max);
}
export function sampleArray(array, n) {
if(n >= array.length)
return array.slice(); // Return a copy of the array
let result = array.slice(); // Create a copy of the array
shuffleArray(result); // Shuffle the copy
return result.slice(0, n); // Return the first n elements
}
export function seed256() {
const array = new Uint32Array(8);
window.crypto.getRandomValues(array);
// Convert to hex
return Array.from(array).map(x => x.toString(16).padStart(8, '0')).join('');
}