diff options
| author | realtradam <[email protected]> | 2023-04-12 15:55:33 -0400 |
|---|---|---|
| committer | realtradam <[email protected]> | 2023-04-12 15:55:33 -0400 |
| commit | 0841165881871ee01b782129be681209aeed2423 (patch) | |
| tree | 8a76b61dcaab381b6b42305201ae8b6259f6b6c0 /misc/examples/gauss2.c | |
| parent | 554f3e8acf7855b5d6a90cc68cefb7445460b03c (diff) | |
| parent | 0516aa3ae823ed9a22b2c5f776948c8447c32c31 (diff) | |
| download | STC-modified-0841165881871ee01b782129be681209aeed2423.tar.gz STC-modified-0841165881871ee01b782129be681209aeed2423.zip | |
Merge branch 'master' into modified
Diffstat (limited to 'misc/examples/gauss2.c')
| -rw-r--r-- | misc/examples/gauss2.c | 44 |
1 files changed, 23 insertions, 21 deletions
diff --git a/misc/examples/gauss2.c b/misc/examples/gauss2.c index c531e5e3..df709d03 100644 --- a/misc/examples/gauss2.c +++ b/misc/examples/gauss2.c @@ -1,42 +1,44 @@ #include <stdio.h> #include <time.h> -#include <stc/crandom.h> +#include <stc/crand.h> #include <stc/cstr.h> // Declare int -> int sorted map. #define i_key int -#define i_val size_t +#define i_val int #include <stc/csmap.h> int main() { - enum {N = 10000000}; - const double Mean = -12.0, StdDev = 6.0, Scale = 74; + enum {N = 5000000}; + uint64_t seed = (uint64_t)time(NULL); + crand_t rng = crand_init(seed); + const double Mean = round(crand_f64(&rng)*98.f - 49.f), StdDev = crand_f64(&rng)*10.f + 1.f, Scale = 74.f; printf("Demo of gaussian / normal distribution of %d random samples\n", N); + printf("Mean %f, StdDev %f\n", Mean, StdDev); // Setup random engine with normal distribution. - uint64_t seed = (uint64_t)time(NULL); - stc64_t rng = stc64_new(seed); - stc64_normalf_t dist = stc64_normalf_new(Mean, StdDev); + crand_norm_t dist = crand_norm_init(Mean, StdDev); // Create and init histogram map with defered destruct - c_auto (csmap_int, mhist) - { - c_forrange (N) { - int index = (int) round( stc64_normalf(&rng, &dist) ); - csmap_int_insert(&mhist, index, 0).ref->second += 1; - } + csmap_int hist = {0}; + cstr bar = {0}; + + c_forrange (N) { + int index = (int)round(crand_norm(&rng, &dist)); + csmap_int_insert(&hist, index, 0).ref->second += 1; + } - // Print the gaussian bar chart - c_auto (cstr, bar) - c_forpair (index, count, csmap_int, mhist) { - int n = (int)((float)*_.count * StdDev * Scale * 2.5f / (float)N); - if (n > 0) { - cstr_resize(&bar, n, '*'); - printf("%4d %s\n", *_.index, cstr_str(&bar)); - } + // Print the gaussian bar chart + c_forpair (index, count, csmap_int, hist) { + int n = (int)round((double)*_.count * StdDev * Scale * 2.5 / (double)N); + if (n > 0) { + cstr_resize(&bar, n, '*'); + printf("%4d %s\n", *_.index, cstr_str(&bar)); } } + cstr_drop(&bar); + csmap_int_drop(&hist); } |
