-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomNumberService.js
More file actions
69 lines (57 loc) · 2.05 KB
/
Copy pathrandomNumberService.js
File metadata and controls
69 lines (57 loc) · 2.05 KB
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
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const POOL_SIZE = 260_000; // Equivalent to one archived .bin file of 1 MB file size
const RANDOM_NUMBERS_FILE = path.join(__dirname, 'random.org-pregenerated-2025-04-bin', '2025-04-14.bin');
let randomNumberPool = [];
// Helper function to load random numbers from binary file
function loadRandomNumbers() {
const buffer = fs.readFileSync(RANDOM_NUMBERS_FILE);
const numbers = [];
for (let i = 0; i < buffer.length; i += 4) {
numbers.push(buffer.readUInt32BE(i));
}
return numbers;
}
// Shuffle function
function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
const randomBytes = crypto.randomBytes(4);
const randomValue = randomBytes.readUInt32BE(0);
const j = randomValue % (i + 1);
[array[i], array[j]] = [array[j], array[i]];
}
}
// Initialize pool on startup
function initializePool() {
const numbers = loadRandomNumbers();
randomNumberPool = numbers.slice(0, POOL_SIZE);
shuffle(randomNumberPool);
}
// Generate random numbers
function generateRandomNumbers(n, min, max, replacement) {
if (replacement) {
return Array.from({ length: n }, () => {
const randomIndex = Math.floor(Math.random() * randomNumberPool.length);
return Math.floor(min + (randomNumberPool[randomIndex] % (max - min + 1)));
});
} else {
if (n > randomNumberPool.length) {
throw new Error('Not enough unique numbers available in the pool.');
}
const result = [];
const usedIndices = new Set();
while (result.length < n) {
const randomIndex = Math.floor(Math.random() * randomNumberPool.length);
if (!usedIndices.has(randomIndex)) {
usedIndices.add(randomIndex);
result.push(Math.floor(min + (randomNumberPool[randomIndex] % (max - min + 1))));
}
}
return result;
}
}
module.exports = {
initializePool,
generateRandomNumbers
};