pastebin

Paste Search Dynamic
Recent pastes
simulate multiple families
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define NUM_SIMULATIONS 1000
  6.  
  7. void simulate_multiple_families(int num_simulations, double *probabilities_bb) {
  8.     int bb_count = 0;
  9.     int at_least_one_boy_count = 0;
  10.  
  11.     for (int i = 0; i < num_simulations; i++) {
  12.         int child1 = rand() % 2; // 0 represents girl, 1 represents boy
  13.         int child2 = rand() % 2; // 0 represents girl, 1 represents boy
  14.  
  15.         if (child1 == 1 && child2 == 1) {
  16.             bb_count++;
  17.         }
  18.  
  19.         if (child1 == 1 || child2 == 1) {
  20.             at_least_one_boy_count++;
  21.         }
  22.  
  23.         double probability_bb = (double) bb_count / at_least_one_boy_count;
  24.         probabilities_bb[i] = probability_bb;
  25.     }
  26. }
  27.  
  28. int main() {
  29.     srand(time(null)); // Initialize random number generator with current time
  30.  
  31.     double probabilities_bb[NUM_SIMULATIONS];
  32.     simulate_multiple_families(NUM_SIMULATIONS, probabilities_bb);
  33.  
  34.     FILE *gnuplot = popen("gnuplot -persistent", "w");
  35.     fprintf(gnuplot, "set terminal pngn");
  36.     fprintf(gnuplot, "set output 'probability_graph.png'n");
  37.     fprintf(gnuplot, "set xlabel 'Number of Simulations'n");
  38.     fprintf(gnuplot, "set ylabel 'Probability'n");
  39.     fprintf(gnuplot, "plot '-' with linesn");
  40.  
  41.     for (int i = 0; i < NUM_SIMULATIONS; i++) {
  42.         fprintf(gnuplot, "%d %lfn", i + 1, probabilities_bb[i]);
  43.     }
  44.  
  45.     fprintf(gnuplot, "en");
  46.     fflush(gnuplot);
  47.  
  48.     pclose(gnuplot);
  49.  
  50.     return 0;
  51. }
  52.  
Parsed in 0.034 seconds