normalen.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Copyright (C) 1994. James Darrell McCauley. (darrell@mccauley-usa.com)
  3. * http://mccauley-usa.com/
  4. *
  5. * This program is free software under the GPL (>=v2)
  6. * Read the file GPL.TXT coming with GRASS for details.
  7. */
  8. #include<stdio.h>
  9. #include<math.h>
  10. #include"zufall.h"
  11. /* Box-Muller method for Gaussian random numbers */
  12. int normalen(int n, double *x)
  13. {
  14. static int buffsz = 1024;
  15. extern struct klotz1 klotz1_1;
  16. int left, i, nn, ptr;
  17. /* Parameter adjustments */
  18. --x;
  19. nn = n;
  20. if (nn <= 0)
  21. return 0;
  22. if (klotz1_1.first == 0) {
  23. normal00();
  24. klotz1_1.first = 1;
  25. }
  26. ptr = 0;
  27. L1:
  28. left = buffsz - klotz1_1.xptr;
  29. if (nn < left) {
  30. for (i = 1; i <= nn; ++i)
  31. x[i + ptr] = klotz1_1.xbuff[klotz1_1.xptr + i - 1];
  32. klotz1_1.xptr += nn;
  33. return 0;
  34. }
  35. else {
  36. for (i = 1; i <= left; ++i)
  37. x[i + ptr] = klotz1_1.xbuff[klotz1_1.xptr + i - 1];
  38. klotz1_1.xptr = 0;
  39. ptr += left;
  40. nn -= left;
  41. normal00();
  42. goto L1;
  43. }
  44. } /* normalen */