summaryrefslogtreecommitdiffhomepage
path: root/examples/gauss2.c
blob: 6bbd7fb37c23c6fbc23bf37fe00111d99e954063 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <time.h>

#define STC_IMPLEMENT
#include <stc/crandom.h>
#include <stc/cstr.h>

// Declare int -> int sorted map.
#define i_key int
#define i_val size_t
#include <stc/csmap.h>

int main()
{
    enum {N = 10000000};
    const double Mean = -12.0, StdDev = 6.0, Scale = 74;

    printf("Demo of gaussian / normal distribution of %d random samples\n", N);

    // Setup random engine with normal distribution.
    uint64_t seed = time(NULL);
    stc64_t rng = stc64_new(seed);
    stc64_normalf_t dist = stc64_normalf_new(Mean, StdDev);

    // Create and init histogram map with defered destruct
    c_auto (csmap_int, mhist)
    {
        c_forloop (N) {
            int index = (int) round( stc64_normalf(&rng, &dist) );
            csmap_int_insert(&mhist, index, 0).ref->second += 1;
        }

        // Print the gaussian bar chart
        c_auto (cstr, bar)
        c_forpair (index, count, csmap_int, mhist) {
            size_t n = (size_t) (*_.count * StdDev * Scale * 2.5 / (float)N);
            if (n > 0) {
                cstr_resize(&bar, n, '*');
                printf("%4d %s\n", *_.index, cstr_str(&bar));
            }
        }
    }
}