Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rounding error produces non-uniform randomness #1

Open
Cortexelus opened this issue Jan 23, 2020 · 0 comments
Open

Rounding error produces non-uniform randomness #1

Cortexelus opened this issue Jan 23, 2020 · 0 comments

Comments

@Cortexelus
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant