diff options
| author | Tyge Løvset <[email protected]> | 2021-11-25 22:14:11 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2021-11-25 22:14:11 +0100 |
| commit | f857d8215a266673e25356779f740100fe362025 (patch) | |
| tree | f044e6435fd31c626078a1e04618eaac080f24fe /examples | |
| parent | 9da78656f7b4a756dad2bf1c285a61a5186fd5ae (diff) | |
| download | STC-modified-f857d8215a266673e25356779f740100fe362025.tar.gz STC-modified-f857d8215a266673e25356779f740100fe362025.zip | |
Updated int params constness. Updated random.c example.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/crandom_ex.c | 51 | ||||
| -rw-r--r-- | examples/random.c | 43 |
2 files changed, 17 insertions, 77 deletions
diff --git a/examples/crandom_ex.c b/examples/crandom_ex.c deleted file mode 100644 index 1d1691af..00000000 --- a/examples/crandom_ex.c +++ /dev/null @@ -1,51 +0,0 @@ -#include <stdio.h>
-#include <time.h>
-#include <math.h>
-#include <stc/crandom.h>
-#include <stc/cstr.h>
-
-int main()
-{
- enum {R = 30};
- const size_t N = 1000000000;
- uint64_t seed = 1234; // time(NULL);
- stc64_t rng = stc64_init(seed);
-
- uint64_t sum = 0;
-
- stc64_normalf_t dist2 = stc64_normalf_init((float)R / 2.0, (float)R / 6.0);
- size_t N2 = 10000000;
- int hist[R] = {0};
- sum = 0;
- c_forrange (N2) {
- int n = round((stc64_normalf(&rng, &dist2) + 0.5));
- sum += n;
- if (n >= 0 && n < R) ++hist[n];
- }
- cstr bar = cstr_init();
- c_forrange (i, int, R) {
- cstr_resize(&bar, hist[i] * 25ull * R / N2, '*');
- printf("%3d %s\n", i, bar.str);
- }
-
- clock_t diff, before;
-
- sum = 0;
- before = clock();
- c_forrange (N) {
- sum += stc64_rand(&rng);
- }
- diff = clock() - before;
- printf("random : %f secs, %zu %f\n", (float) diff / CLOCKS_PER_SEC, N, (double) sum / N);
-
- stc64_uniform_t dist1 = stc64_uniform_init(0, 1000);
- sum = 0;
- before = clock();
- c_forrange (N) {
- sum += stc64_uniform(&rng, &dist1);
- }
- diff = clock() - before;
- printf("uniform: %f secs, %zu %f\n", (float) diff / CLOCKS_PER_SEC, N, (double) sum / N);
-
- cstr_del(&bar);
-}
\ No newline at end of file diff --git a/examples/random.c b/examples/random.c index b3b01533..d4a12a27 100644 --- a/examples/random.c +++ b/examples/random.c @@ -1,51 +1,42 @@ #include <stdio.h>
#include <time.h>
-#include <math.h>
#include <stc/crandom.h>
-#include <stc/cstr.h>
int main()
{
- enum {R = 30};
- const size_t N = 1000000000;
- uint64_t seed = 1234; // time(NULL);
+ const size_t N = 5000000000;
+ const uint64_t seed = time(NULL), range = 1000000;
stc64_t rng = stc64_init(seed);
- uint64_t sum = 0;
+ uint64_t sum;
+ clock_t diff, before;
- stc64_normalf_t dist2 = stc64_normalf_init((float)R / 2.0, (float)R / 6.0);
- size_t N2 = 10000000;
- int hist[R] = {0};
+ printf("Compare speed of full and unbiased ranged random numbers...\n");
sum = 0;
- c_forrange (N2) {
- int n = (int) round((stc64_normalf(&rng, &dist2) + 0.5));
- sum += n;
- if (n >= 0 && n < R) ++hist[n];
- }
- cstr bar = cstr_init();
- c_forrange (i, int, R) {
- cstr_resize(&bar, hist[i] * 25ull * R / N2, '*');
- printf("%3d %s\n", i, bar.str);
+ before = clock();
+ c_forrange (N) {
+ sum += (uint32_t) stc64_rand(&rng);
}
+ diff = clock() - before;
+ printf("full range\t\t: %f secs, %zu, avg: %f\n", (float) diff / CLOCKS_PER_SEC, N, (double) sum / N);
- clock_t diff, before;
-
+ stc64_uniform_t dist1 = stc64_uniform_init(0, range);
+ rng = stc64_init(seed);
sum = 0;
before = clock();
c_forrange (N) {
- sum += stc64_rand(&rng);
+ sum += stc64_uniform(&rng, &dist1); // unbiased
}
diff = clock() - before;
- printf("random : %f secs, %zu %f\n", (float) diff / CLOCKS_PER_SEC, N, (double) sum / N);
+ printf("unbiased 0-%zu\t: %f secs, %zu, avg: %f\n", range, (float) diff / CLOCKS_PER_SEC, N, (double) sum / N);
- stc64_uniform_t dist1 = stc64_uniform_init(0, 1000);
sum = 0;
+ rng = stc64_init(seed);
before = clock();
c_forrange (N) {
- sum += stc64_uniform(&rng, &dist1);
+ sum += stc64_rand(&rng) % (range + 1); // biased
}
diff = clock() - before;
- printf("uniform: %f secs, %zu %f\n", (float) diff / CLOCKS_PER_SEC, N, (double) sum / N);
+ printf("biased 0-%zu \t: %f secs, %zu, avg: %f\n", range, (float) diff / CLOCKS_PER_SEC, N, (double) sum / N);
- cstr_del(&bar);
}
\ No newline at end of file |
