gauss.c 649 B

12345678910111213141516171819202122232425262728293031323334
  1. #include <math.h>
  2. #include <grass/gmath.h>
  3. /*!
  4. * \fn double G_math_rand_gauss(const int seed, const double sigma)
  5. *
  6. * \brief Gaussian random number generator
  7. *
  8. * Gaussian random number generator (mean = 0.0)
  9. *
  10. * \param[in] seed
  11. & \param[in] sigma
  12. * \return double
  13. */
  14. double G_math_rand_gauss(double sigma)
  15. {
  16. double x, y, r2;
  17. do {
  18. /* choose x,y in uniform square (-1,-1) to (+1,+1) */
  19. x = -1 + 2 * G_math_rand();
  20. y = -1 + 2 * G_math_rand();
  21. /* see if it is in the unit circle */
  22. r2 = x * x + y * y;
  23. }
  24. while (r2 > 1.0 || r2 == 0);
  25. /* Box-Muller transform */
  26. return sigma * y * sqrt(-2.0 * log(r2) / r2);
  27. }