Not very random
gajus opened this issue · comments
Gajus Kuizinas commented
#!/usr/bin/env node
import Srand from 'seeded-rand';
let seed = 0;
let last;
while (true) {
const next = new Srand(seed++).intInRange(1, 20);
console.log(seed, next);
if (last && last !== next) {
break;
}
last = next;
}
1 10
2 10
3 10
[..]
10264 10
10265 10
10266 10
10267 10
10268 10
10269 10
10270 10
10271 10
10272 10
10273 10
10274 10
10275 10
10276 10
10277 10
10278 10
10279 10
10280 10
10281 10
10282 10
10283 11
Gajus Kuizinas commented
This can be mitigated by hashing the seed:
import {
createHash,
} from 'crypto';
import Srand from 'seeded-rand';
const seed = createHash('sha1').update('1').digest().readUInt32BE();
new Srand(seed).intInRange(1, 20);
Might be worth to mention it in the docs. :-)