summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-01-17 18:09:31 +0100
committerTyge Løvset <[email protected]>2021-01-17 18:09:31 +0100
commitc53d2f0603ae86447e98b697c3d86d710be8ac2d (patch)
tree026a0f8bde8dab99f5f17295ebed4d988a529728 /examples
parent7c4dc9f1b2b73bdaba6a7884209db73df8a22d16 (diff)
downloadSTC-modified-c53d2f0603ae86447e98b697c3d86d710be8ac2d.tar.gz
STC-modified-c53d2f0603ae86447e98b697c3d86d710be8ac2d.zip
Fixed value offset bug. Added gauss example with csmap.
Diffstat (limited to 'examples')
-rw-r--r--examples/csmap_ex.c2
-rw-r--r--examples/ex_gauss1.c (renamed from examples/ex_gaussian.c)0
-rw-r--r--examples/ex_gauss2.c42
3 files changed, 43 insertions, 1 deletions
diff --git a/examples/csmap_ex.c b/examples/csmap_ex.c
index cd623b06..14d21b67 100644
--- a/examples/csmap_ex.c
+++ b/examples/csmap_ex.c
@@ -3,7 +3,7 @@
#include <stc/crand.h>
#include <stdio.h>
-using_csmap(i, int, int);
+using_csmap(i, int, size_t);
using_csset_str();
#include <time.h>
diff --git a/examples/ex_gaussian.c b/examples/ex_gauss1.c
index b9228aa5..b9228aa5 100644
--- a/examples/ex_gaussian.c
+++ b/examples/ex_gauss1.c
diff --git a/examples/ex_gauss2.c b/examples/ex_gauss2.c
new file mode 100644
index 00000000..7352d6a8
--- /dev/null
+++ b/examples/ex_gauss2.c
@@ -0,0 +1,42 @@
+#include <stdio.h>
+#include <time.h>
+#include <math.h>
+#include "stc/crand.h"
+#include "stc/cstr.h"
+#include "stc/csmap.h"
+
+// Declare int -> int sorted map. Uses typetag 'i' for ints.
+using_csmap(i, int, size_t);
+
+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_init(seed);
+ stc64_normalf_t dist = stc64_normalf_init(Mean, StdDev);
+
+ // Create histogram map
+ csmap_i mhist = csmap_i_init();
+ for (size_t i = 0; i < N; ++i) {
+ int index = (int) round( stc64_normalf(&rng, &dist) );
+ ++ csmap_i_emplace(&mhist, index, 0).first->second;
+ }
+
+ // Print the gaussian bar chart
+ cstr_t bar = cstr_init();
+ c_foreach (i, csmap_i, mhist) {
+ size_t n = (size_t) (i.ref->second * StdDev * Scale * 2.5 / N);
+ if (n > 0) {
+ cstr_resize(&bar, n, '*');
+ printf("%4d %s\n", i.ref->first, bar.str);
+ }
+ }
+ // Cleanup
+ cstr_del(&bar);
+ csmap_i_del(&mhist);
+}