Skip to content

Instantly share code, notes, and snippets.

@trevphil
Last active June 3, 2019 12:51
Show Gist options
  • Save trevphil/1b885ac68f70b24ce44e52d2cf81565b to your computer and use it in GitHub Desktop.
Save trevphil/1b885ac68f70b24ce44e52d2cf81565b to your computer and use it in GitHub Desktop.
Sampling from normal distribution
private func sampleNormalDist(withMean mean: Double, std: Double) -> Double {
precondition(std > 0)
let first = Double(rng.nextUniform())
let second = Double(rng.nextUniform())
let zScore = sqrt(-2 * log(first)) * cos(2 * Double.pi * second)
return zScore * std + mean
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment