Skip to content

Commit c88d4e3

Browse files
committed
Améliorer le constructeur de RandomEngine pour un meilleur comportement aléatoire et ajouter un support multiplateforme pour cryptoInt
1 parent 8af0632 commit c88d4e3

File tree

2 files changed

+124
-107
lines changed

2 files changed

+124
-107
lines changed

algorithms/RandomEngine.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// randomEngine.js - moteur d'aléatoire avancé + bruit
22

33
class RandomEngine {
4-
constructor(seed = Date.now()) {
4+
constructor(seed = Date.now() + Math.random() * 1000) {
55
this.seed = seed;
66
this.rand = this.mulberry32(seed);
77
this.generateGradientTable();
@@ -109,7 +109,24 @@ class RandomEngine {
109109
static cryptoInt(min, max) {
110110
const range = max - min + 1;
111111
const rand = new Uint32Array(1);
112-
crypto.getRandomValues(rand);
112+
113+
// Support cross-platform crypto
114+
if (typeof crypto !== 'undefined' && crypto.getRandomValues) {
115+
crypto.getRandomValues(rand);
116+
} else if (typeof require !== 'undefined') {
117+
try {
118+
const crypto = require('crypto');
119+
const buffer = crypto.randomBytes(4);
120+
rand[0] = buffer.readUInt32BE(0);
121+
} catch (e) {
122+
// Fallback pour les environnements sans crypto
123+
rand[0] = Math.floor(Math.random() * 4294967296);
124+
}
125+
} else {
126+
// Fallback ultime
127+
rand[0] = Math.floor(Math.random() * 4294967296);
128+
}
129+
113130
return min + (rand[0] % range);
114131
}
115132

0 commit comments

Comments
 (0)