feat(core): add gauss function to Random - #709
Merged
Merged
Conversation
`gauss(mean, stdev)` will return a normally distributed random value with a given `mean` and `stdev`. The implementation was ported to TypeScript from Python's `random.gauss()`: https://docs.python.org/3/library/random.html#random.gauss It includes a small optimization that caches a second independent random value since the function always generates a pair.
ajs1998
commented
May 23, 2023
ajs1998
commented
May 23, 2023
Comment on lines
+53
to
+63
| public gauss(mean = 0.0, stdev = 1.0): number { | ||
| let z = this.nextGauss; | ||
| this.nextGauss = null; | ||
| if (z === null) { | ||
| const x2pi = this.next() * 2 * Math.PI; | ||
| const g2rad = Math.sqrt(-2.0 * Math.log(1.0 - this.next())); | ||
| z = Math.cos(x2pi) * g2rad; | ||
| this.nextGauss = Math.sin(x2pi) * g2rad; | ||
| } | ||
| return mean + z * stdev; | ||
| } |
Contributor
Author
There was a problem hiding this comment.
For reference, this is Python's implementation:
def gauss(self, mu=0.0, sigma=1.0):
random = self.random
z = self.gauss_next
self.gauss_next = None
if z is None:
x2pi = random() * TWOPI
g2rad = _sqrt(-2.0 * _log(1.0 - random()))
z = _cos(x2pi) * g2rad
self.gauss_next = _sin(x2pi) * g2rad
return mu + z * sigma
aarthificial
requested changes
May 23, 2023
…als instead of decimals
Contributor
Author
aarthificial
approved these changes
May 23, 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.


gauss(mean, stdev)will return a normally distributed random value with a givenmeanandstdev.The implementation was ported to TypeScript from Python's
random.gauss(): https://docs.python.org/3/library/random.html#random.gaussIt includes a small optimization that caches a second independent random value
since the function always generates a pair.
Closes #704