summaryrefslogtreecommitdiffhomepage
path: root/docs/crandom_api.md
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-01-21 11:31:46 +0100
committerTyge Løvset <[email protected]>2021-01-21 11:31:46 +0100
commitc52942dfe456c15567d79128e5059c14ab1903ea (patch)
tree77d624f569625bca272dd59d6e3bc4e5f98864a2 /docs/crandom_api.md
parentff84705c03e06ad2f44396719253fe0fb8112171 (diff)
parent2ec41cca7eca5b1e3a9bf83c6cc9afcb9bb7a82d (diff)
downloadSTC-modified-c52942dfe456c15567d79128e5059c14ab1903ea.tar.gz
STC-modified-c52942dfe456c15567d79128e5059c14ab1903ea.zip
Merge branch 'master' of https://github.com/tylo-work/C99Containers into master
Diffstat (limited to 'docs/crandom_api.md')
-rw-r--r--docs/crandom_api.md39
1 files changed, 11 insertions, 28 deletions
diff --git a/docs/crandom_api.md b/docs/crandom_api.md
index 1e93c4af..df5c3497 100644
--- a/docs/crandom_api.md
+++ b/docs/crandom_api.md
@@ -19,7 +19,7 @@ is generated when the Weyl-increment is incremented by 2 every 2^64 output.
**stc64** passes *PractRand*, tested up to 8TB output, Vigna's Hamming weight test, and simple
correlation tests, i.e. *n* interleaved streams with only one-bit differences in initial state.
-See the PRNG shootout by Vigna: http://prng.di.unimi.it and the debate between the authors of
+For more, see the PRNG shootout by Vigna: http://prng.di.unimi.it and the debate between the authors of
xoshiro and pcg (Vigna/O'Neill) PRNGs: https://www.pcg-random.org/posts/on-vignas-pcg-critique.html
## Types
@@ -67,22 +67,13 @@ RNG, around 68% of the values fall within the range [*mean* - *stddev*, *mean* +
```c
#include <stdio.h>
#include <time.h>
-#include <math.h>
-#include "stc/crandom.h"
-#include "stc/cstr.h"
-#include "stc/cmap.h"
-#include "stc/cvec.h"
-
-// Declare unordered map: int -> int with typetag 'i'.
-using_cmap(i, int, size_t);
-// Comparison of map keys.
-static int compare(cmap_i_value_t *a, cmap_i_value_t *b) {
- return c_default_compare(&a->first, &b->first);
-}
-// Declare vector of map entries, with comparison function.
-using_cvec(e, cmap_i_value_t, compare);
+#include <stc/crandom.h>
+#include <stc/csmap.h>
+#include <stc/cstr.h>
+// Declare int -> int sorted map. Uses typetag 'i' for ints.
+using_csmap(i, int, size_t);
int main()
{
@@ -97,32 +88,24 @@ int main()
stc64_normalf_t dist = stc64_normalf_init(Mean, StdDev);
// Create histogram map
- cmap_i mhist = cmap_i_init();
- for (size_t i = 0; i < N; ++i) {
+ csmap_i mhist = csmap_i_init();
+ c_forrange (N) {
int index = (int) round( stc64_normalf(&rng, &dist) );
- cmap_i_emplace(&mhist, index, 0).first->second += 1;
+ ++ csmap_i_emplace(&mhist, index, 0).first->second;
}
- // Transfer map to vec and sort it by map entry keys.
- cvec_e vhist = cvec_e_init();
- c_foreach (i, cmap_i, mhist)
- cvec_e_push_back(&vhist, *i.ref);
- cvec_e_sort(&vhist);
-
// Print the gaussian bar chart
cstr_t bar = cstr_init();
- c_foreach (i, cvec_e, vhist) {
+ 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);
- cmap_i_del(&mhist);
- cvec_e_del(&vhist);
+ csmap_i_del(&mhist);
}
```
Output: