quantum-dice icon indicating copy to clipboard operation
quantum-dice copied to clipboard

Rounding error produces non-uniform randomness

Open Cortexelus opened this issue 5 years ago • 0 comments

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: image

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

nor does d20 evenly divide 32 image

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: image

Cool project 👍 Best, CJ

Cortexelus avatar Jan 23 '20 13:01 Cortexelus