Advertisement
Guest User

Gaussian distribution

a guest
Aug 29th, 2012
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. /* utils.h begins */
  2.  
  3. #include <stdlib.h>
  4.  
  5. extern int d (int n); /* n-sided die. */
  6.  
  7. /* utils.h ends */
  8.  
  9.  
  10.  
  11. /* utils.c begins */
  12.  
  13. #include "utils.h"
  14.  
  15. int
  16. d (int n) { /* Given n, throw an n-sided die. */
  17. return 1 + (rand () / (RAND_MAX / n + 1));
  18. }
  19.  
  20. /* utils.c ends */
  21.  
  22.  
  23.  
  24. /* gauss.h begins */
  25.  
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <time.h>
  29.  
  30. #include "utils.h"
  31.  
  32.  
  33. #define NDICE 3 /* The number of dice to be rolled. */
  34. #define SIDES 6 /* Each die has these many sides. */
  35. #define MAXSUM SIDES * NDICE /* The maximum possible sum of NDICE
  36. dice.
  37. */
  38. #define LIMIT (MAXSUM) + 1 /* This constant is used:
  39. 0) to declare the array of sums
  40. 1) as a loop limit
  41. */
  42. #define ROLLS 10000 /* The number of throws of the dice */
  43. #define SCALE 100 /* Used to fit the histogram into the
  44. screen width. A lower value
  45. implies longer bars.
  46. */
  47.  
  48. extern void initsums (int sums [], int limit);
  49. extern int sumdice (int ndice);
  50. extern void fillsums (int sums [], int maxrolls);
  51. extern void printsums (int sums [], int limit);
  52. extern void histogram (int sum, int n);
  53. extern void histsums (int sums [], int limit);
  54.  
  55. /* gauss.h ends */
  56.  
  57.  
  58.  
  59. /* gauss.c begins */
  60.  
  61. #include "gauss.h"
  62.  
  63. /* Initialize only the relevant indices to 0. So, care must be taken
  64. not to try using indices 0 and NDICE - 1.
  65. */
  66. void
  67. initsums (int sums [], int limit) {
  68. int i;
  69.  
  70. for (i = NDICE; i < limit; i++)
  71. sums [i] = 0;
  72. }
  73.  
  74. /* Roll the given number of dice and return their sum. */
  75. int
  76. sumdice (int ndice) {
  77. int i;
  78. int sum = 0;
  79.  
  80. for (i = 0; i < ndice; i++)
  81. sum += d (SIDES);
  82.  
  83. return sum;
  84. }
  85.  
  86. /* For the specified number of dice throws: calculate the sum of the
  87. given number of dice, and increment the array slot whose index is
  88. the sum.
  89. */
  90. void
  91. fillsums (int sums [], int maxrolls) {
  92. int sum; /* Sum of the dice. */
  93. int i;
  94.  
  95. for (i = 0; i < maxrolls; i++) {
  96. sum = sumdice (NDICE);
  97. ++sums [sum];
  98. }
  99. }
  100.  
  101. /* Print the number of occurrences of the various sums. */
  102. void
  103. printsums (int sums [], int limit) {
  104. int i;
  105.  
  106. for (i = NDICE; i < limit; i++)
  107. printf ("%2d: %d\n", i, sums [i]);
  108. }
  109.  
  110. /* Print a horizontal bar histogram for a given sum. */
  111. void
  112. histogram (int sum, int n) {
  113. int i;
  114.  
  115. printf ("%2d: ", sum);
  116. for (i = 0; i < n / SCALE; i++)
  117. putchar ('=');
  118. putchar ('\n');
  119. }
  120.  
  121. /* Print a histogram of the occurrences. */
  122. void
  123. histsums (int sums [], int limit) {
  124. int i;
  125.  
  126. if ((NDICE > 0) && (SIDES > 1)) {
  127. for (i = NDICE; i < limit; i++)
  128. histogram (i, sums [i]);
  129. }
  130. else {
  131. if (NDICE < 1) {
  132. fputs ("NDICE set to insane value\n", stderr);
  133. fputs ("So, no histogram.\n", stderr);
  134. }
  135. if (SIDES < 2) {
  136. fputs ("SIDES set to insane value\n", stderr);
  137. fputs ("So, no histogram.\n", stderr);
  138. }
  139. }
  140. }
  141.  
  142. /* gauss.c ends */
  143.  
  144.  
  145.  
  146. /* main.c begins */
  147.  
  148. #include "gauss.h"
  149.  
  150. int
  151. main (void) {
  152. int sums [LIMIT]; /* We use only indices
  153. NDICE to LIMIT - 1
  154. for indexing the sums of the given
  155. number of dice. We waste
  156. sizeof (int) * NDICE
  157. bytes.
  158. */
  159.  
  160. srand (time (NULL)); /* Seed the RNG using the current
  161. time.
  162. */
  163.  
  164. initsums (sums, LIMIT); /* Initialize the (relevant) indices
  165. to 0.
  166. */
  167. fillsums (sums, ROLLS); /* Count the sums. */
  168. printsums (sums, LIMIT); /* Print how many of each sum
  169. counted. */
  170. putchar ('\n');
  171. histsums (sums, LIMIT); /* Print a histogram of the same. */
  172.  
  173. exit (EXIT_SUCCESS); /* Done. So, exit. */
  174. }
  175.  
  176. /* main.c ends */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement