mirror of
https://github.com/quantum5/correcthorsebatterystaple.git
synced 2025-04-24 10:11:57 -04:00
Fix non-power-of-two random number generation
This commit is contained in:
parent
9154c5210a
commit
e157c32f9f
|
@ -10,6 +10,7 @@ function getWordList (name) {
|
||||||
throw new Error(`Invalid word list: ${name}`)
|
throw new Error(`Invalid word list: ${name}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Only works when the maximum possible value in indices is divisible by list.length.
|
||||||
function getWords (list, indices) {
|
function getWords (list, indices) {
|
||||||
return Array.from(indices).map(index => list[index % list.length])
|
return Array.from(indices).map(index => list[index % list.length])
|
||||||
}
|
}
|
||||||
|
@ -18,21 +19,25 @@ function capitalize (string) {
|
||||||
return string[0].toUpperCase() + string.slice(1)
|
return string[0].toUpperCase() + string.slice(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Only works on power-of-two sized lists.
|
||||||
function pickWords (list, number) {
|
function pickWords (list, number) {
|
||||||
const array = new Uint16Array(number)
|
const array = new Uint16Array(number)
|
||||||
window.crypto.getRandomValues(array)
|
window.crypto.getRandomValues(array)
|
||||||
return getWords(list, array)
|
return getWords(list, array)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Only works when the maximum possible value of index is divisible by choices.length.
|
||||||
function getChar (choices, index) {
|
function getChar (choices, index) {
|
||||||
return choices[index % choices.length]
|
return choices[index % choices.length]
|
||||||
}
|
}
|
||||||
|
|
||||||
function pickChar (choices) {
|
function pickChar (choices) {
|
||||||
const array = new Uint32Array(1)
|
const array = new Uint32Array(1)
|
||||||
|
const maxValid = Math.floor(4294967296 / choices.length) * choices.length;
|
||||||
|
do {
|
||||||
window.crypto.getRandomValues(array)
|
window.crypto.getRandomValues(array)
|
||||||
const index = array[0]
|
} while (array[0] >= maxValid)
|
||||||
return getChar(choices, index)
|
return getChar(choices, array[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
function generate (options) {
|
function generate (options) {
|
||||||
|
|
Loading…
Reference in a new issue