Skip to content

Commit a98b5b6

Browse files
committed
Random num fix
1 parent 7ade628 commit a98b5b6

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

  • apps/javascript/src/challenges/password-generator

apps/javascript/src/challenges/password-generator/index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,17 @@ const APLHABETWORDS = {
9494
};
9595

9696
const secureRandomIndex = (length) => {
97-
if (length <= 0) return 0;
98-
return crypto.getRandomValues(new Uint32Array(1))[0] % length;
97+
if (length <= 1) return 0;
98+
99+
const buckets = Math.floor(0x100000000 / length);
100+
const limit = buckets * length;
101+
const buf = new Uint32Array(1);
102+
let value;
103+
do {
104+
crypto.getRandomValues(buf);
105+
value = buf[0];
106+
} while (value >= limit);
107+
return Math.floor(value / buckets);
99108
};
100109

101110
const functionMap = {

0 commit comments

Comments
 (0)