quantum-dice
quantum-dice copied to clipboard
Rounding error produces non-uniform randomness
These lines produce a quantization/rounding error
var normalizedRoll = (diceCorpus[index] + 1)/maxDiceRoll //its the sequence thats random, not the number. normalize the sequence for use by any dice
result = Math.ceil(normalizedRoll * finalDiceSize);
Because normalizedRoll is quantized to 32 possible values, if finalDiceSize doesn't divide evenly into 32, then rounding up biases the result
Let's do 1000000 rolls and look at a histogram
d8 divides 32 evenly, so it is uniform:

but d10 does not evenly divide 32. Values of 5 and 10 are more likely:

nor does d20 evenly divide 32

even if we query the quantum computer every roll, instead of cacheing, we still have quantization error. what can we do? hmm.
one way to address this is to just ignore all values larger than finalDiceSize. for example..
while(diceCorpus[index]>=finalDiceSize){
increaseIndex();
}
result = diceCorpus[index] + 1
now look at results for d20:

Cool project 👍 Best, CJ