Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* utils.h begins */
- #include <stdlib.h>
- extern int d (int n); /* n-sided die. */
- /* utils.h ends */
- /* utils.c begins */
- #include "utils.h"
- int
- d (int n) { /* Given n, throw an n-sided die. */
- return 1 + (rand () / (RAND_MAX / n + 1));
- }
- /* utils.c ends */
- /* gauss.h begins */
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include "utils.h"
- #define NDICE 3 /* The number of dice to be rolled. */
- #define SIDES 6 /* Each die has these many sides. */
- #define MAXSUM SIDES * NDICE /* The maximum possible sum of NDICE
- dice.
- */
- #define LIMIT (MAXSUM) + 1 /* This constant is used:
- 0) to declare the array of sums
- 1) as a loop limit
- */
- #define ROLLS 10000 /* The number of throws of the dice */
- #define SCALE 100 /* Used to fit the histogram into the
- screen width. A lower value
- implies longer bars.
- */
- extern void initsums (int sums [], int limit);
- extern int sumdice (int ndice);
- extern void fillsums (int sums [], int maxrolls);
- extern void printsums (int sums [], int limit);
- extern void histogram (int sum, int n);
- extern void histsums (int sums [], int limit);
- /* gauss.h ends */
- /* gauss.c begins */
- #include "gauss.h"
- /* Initialize only the relevant indices to 0. So, care must be taken
- not to try using indices 0 and NDICE - 1.
- */
- void
- initsums (int sums [], int limit) {
- int i;
- for (i = NDICE; i < limit; i++)
- sums [i] = 0;
- }
- /* Roll the given number of dice and return their sum. */
- int
- sumdice (int ndice) {
- int i;
- int sum = 0;
- for (i = 0; i < ndice; i++)
- sum += d (SIDES);
- return sum;
- }
- /* For the specified number of dice throws: calculate the sum of the
- given number of dice, and increment the array slot whose index is
- the sum.
- */
- void
- fillsums (int sums [], int maxrolls) {
- int sum; /* Sum of the dice. */
- int i;
- for (i = 0; i < maxrolls; i++) {
- sum = sumdice (NDICE);
- ++sums [sum];
- }
- }
- /* Print the number of occurrences of the various sums. */
- void
- printsums (int sums [], int limit) {
- int i;
- for (i = NDICE; i < limit; i++)
- printf ("%2d: %d\n", i, sums [i]);
- }
- /* Print a horizontal bar histogram for a given sum. */
- void
- histogram (int sum, int n) {
- int i;
- printf ("%2d: ", sum);
- for (i = 0; i < n / SCALE; i++)
- putchar ('=');
- putchar ('\n');
- }
- /* Print a histogram of the occurrences. */
- void
- histsums (int sums [], int limit) {
- int i;
- if ((NDICE > 0) && (SIDES > 1)) {
- for (i = NDICE; i < limit; i++)
- histogram (i, sums [i]);
- }
- else {
- if (NDICE < 1) {
- fputs ("NDICE set to insane value\n", stderr);
- fputs ("So, no histogram.\n", stderr);
- }
- if (SIDES < 2) {
- fputs ("SIDES set to insane value\n", stderr);
- fputs ("So, no histogram.\n", stderr);
- }
- }
- }
- /* gauss.c ends */
- /* main.c begins */
- #include "gauss.h"
- int
- main (void) {
- int sums [LIMIT]; /* We use only indices
- NDICE to LIMIT - 1
- for indexing the sums of the given
- number of dice. We waste
- sizeof (int) * NDICE
- bytes.
- */
- srand (time (NULL)); /* Seed the RNG using the current
- time.
- */
- initsums (sums, LIMIT); /* Initialize the (relevant) indices
- to 0.
- */
- fillsums (sums, ROLLS); /* Count the sums. */
- printsums (sums, LIMIT); /* Print how many of each sum
- counted. */
- putchar ('\n');
- histsums (sums, LIMIT); /* Print a histogram of the same. */
- exit (EXIT_SUCCESS); /* Done. So, exit. */
- }
- /* main.c ends */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement