Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- float gauss_random(float avg, float sigma)
- {
- static bool haveNextGaussian = false;
- static float nextGaussian = 0.0f;
- if (haveNextGaussian)
- {
- haveNextGaussian = false;
- return nextGaussian * sigma + avg;
- }
- else
- {
- double v1, v2, s;
- do {
- //v1 = 2 * nextDouble() - 1; // between -1.0 and 1.0
- //v2 = 2 * nextDouble() - 1; // between -1.0 and 1.0
- v1 = uniform_random(-1.f, 1.f);
- v2 = uniform_random(-1.f, 1.f);
- s = v1 * v1 + v2 * v2;
- } while (s >= 1.f || fabs(s) < FLT_EPSILON);
- double multiplier = sqrtf(-2 * logf(s) / s);
- nextGaussian = v2 * multiplier;
- haveNextGaussian = true;
- return v1 * multiplier * sigma + avg;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment