improve: normal random skew towards means #1672
Merged
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.
Description
The current code for normal distribution is skewed and tends to generate much more times the central element than the rest of the distribution, more than is expected of a normal distribution.
Comparison of results generating 2^27 random values between 0 and 20
This happens because of the following path:
This causes values below or above two standard deviations (stdev is 0.25) from the mean (0.5) to simply be replaced by the central element. In normal distributions, about 5% of the results will be farther than two standard deviations from the mean, and those would all generate the central element instead.
The proper solution is to just discard the result and generate a new value. The while loop will only run once in 95% of the calls. There is an infinitely small chance of an infinite loop.
The new code is also roughly 30% faster (on a Ryzen 7 4800H chip) due to it performing less branching and having only one return.
credits: @ranisalt