squiggle.c/examples/03_gcc_nested_function/example.c

39 lines
1.1 KiB
C
Raw Normal View History

#include "../../squiggle.h"
2023-07-15 21:22:44 +00:00
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
2023-07-15 21:22:44 +00:00
int main()
{
2023-07-15 21:22:44 +00:00
// set randomness seed
uint64_t* seed = malloc(sizeof(uint64_t));
*seed = 1000; // xorshift can't start with 0
2023-07-15 21:22:44 +00:00
double p_a = 0.8;
double p_b = 0.5;
double p_c = p_a * p_b;
2023-07-15 21:22:44 +00:00
int n_dists = 4;
double sample_0(uint64_t * seed) { return 0; }
double sample_1(uint64_t * seed) { return 1; }
double sample_few(uint64_t * seed) { return sample_to(1, 3, seed); }
double sample_many(uint64_t * seed) { return sample_to(2, 10, seed); }
double (*samplers[])(uint64_t*) = { sample_0, sample_1, sample_few, sample_many };
double weights[] = { 1 - p_c, p_c / 2, p_c / 4, p_c / 4 };
2023-07-15 21:22:44 +00:00
int n_samples = 1000000;
double* result_many = (double*)malloc(n_samples * sizeof(double));
for (int i = 0; i < n_samples; i++) {
result_many[i] = sample_mixture(samplers, weights, n_dists, seed);
}
2023-07-15 21:22:44 +00:00
printf("result_many: [");
for (int i = 0; i < 100; i++) {
printf("%.2f, ", result_many[i]);
}
printf("]\n");
free(seed);
}