Resolve #81 to deal with a potential future scenario of PRNG generating 1
#120
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi @j0pgrm, re #81
Your
if
statement is there to cover the possbility that your PRNG will generate the number 1. In general, PRNGs produce values from 0 to less than 1, i.e. from the range 0 (inclusive) to 1 (exclusive). This is because when they generate an N-bit binary fraction, the highest value (all1
s) will be just a tiny bit smaller than 1.Your
Math.floor
correctly rounds downward, so thatrand
will be in the range 0 (inclusive) to ENCODING_LEN - 1 (inclusive). So theif
is never triggered.If, for some reason, you in future switch to a prng that somehow can generate a 1, for example because it has an idiosyncratic desire to cover a range of 0 (exclusive) to 1 (inclusive), then:
A neat solution for you is to simply wrap round that exactly-1 PRNG value back to 0. A simple way to achieve this is to modulo the
rand
by ENCODING_LEN.Thank you for an excellent library.