squiggle.c

Self-contained Monte Carlo estimation in C99
Log | Files | Refs | README

example.c (1335B)


      1 #include "../../../squiggle.h"
      2 #include "../../../squiggle_more.h"
      3 #include <math.h>
      4 #include <stdio.h>
      5 #include <stdlib.h>
      6 
      7 double sample_minutes_per_day_jumping_rope_needed_to_burn_10kg(uint64_t* seed)
      8 {
      9     double kcal_jumping_rope_minute = sample_to(15, 20, seed);
     10     double kcal_jumping_rope_hour = kcal_jumping_rope_minute * 60;
     11 
     12     double kcal_in_kg_of_fat = 7700;
     13     double num_kg_of_fat_to_lose = 10;
     14 
     15     double hours_jumping_rope_needed = kcal_in_kg_of_fat * num_kg_of_fat_to_lose / kcal_jumping_rope_hour;
     16 
     17     double days_until_end_of_year = 152; // as of 2023-08-01
     18     double hours_per_day = hours_jumping_rope_needed / days_until_end_of_year;
     19     double minutes_per_day = hours_per_day * 60;
     20     return minutes_per_day;
     21 }
     22 
     23 int main()
     24 {
     25     // set randomness seed
     26     uint64_t* seed = malloc(sizeof(uint64_t));
     27     *seed = 1000; // xorshift can't start with 0
     28 
     29     int n = 1000 * 1000;
     30     double* xs = malloc(sizeof(double) * (size_t)n);
     31     for (int i = 0; i < n; i++) {
     32         xs[i] = sample_minutes_per_day_jumping_rope_needed_to_burn_10kg(seed);
     33     }
     34 
     35     printf("## How many minutes per day do I have to jump rope to lose 10kg of fat by the end of the year?\n");
     36 
     37     printf("\n# Stats\n");
     38     array_print_stats(xs, n);
     39     printf("\n# Histogram\n");
     40     array_print_histogram(xs, n, 23);
     41 
     42     free(seed);
     43 }