From a147a1be246472d079f72fc22e065080d1588e29 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 16 Dec 2020 10:58:04 +0100 Subject: Reverted back API to easier naming scheme of crandom.h types and methods. Highly optimized generation of unbiased uniform bounded random numbers. Only 33% overhead. --- examples/benchmark.cpp | 6 +++--- examples/birthday.c | 10 +++++----- examples/ex_gaussian.c | 6 +++--- examples/heap.c | 8 ++++---- examples/list.c | 6 +++--- examples/priority.c | 8 ++++---- examples/queue.c | 10 +++++----- examples/random.c | 16 ++++++++-------- examples/rngtest.c | 14 +++++++------- 9 files changed, 42 insertions(+), 42 deletions(-) (limited to 'examples') diff --git a/examples/benchmark.cpp b/examples/benchmark.cpp index 4830c9ba..206da5ce 100644 --- a/examples/benchmark.cpp +++ b/examples/benchmark.cpp @@ -28,9 +28,9 @@ KHASH_MAP_INIT_INT64(ii, int64_t) size_t seed; static const float max_load_factor = 0.77f; -cstc64_t rng; -#define SEED(s) rng = cstc64_init(seed) -#define RAND(N) (cstc64_rand(&rng) & ((1 << N) - 1)) +crand_t rng; +#define SEED(s) rng = crand_init(seed) +#define RAND(N) (crand_next(&rng) & ((1 << N) - 1)) #define CMAP_SETUP(X, Key, Value) cmap_##X map = cmap_inits \ diff --git a/examples/birthday.c b/examples/birthday.c index fedb2c5c..ac05a5f5 100644 --- a/examples/birthday.c +++ b/examples/birthday.c @@ -14,12 +14,12 @@ const static uint64_t mask = (1ull << 52) - 1; void repeats(void) { - cstc64_t rng = cstc64_init(seed); + crand_t rng = crand_init(seed); cmap_ic m = cmap_ic_init(); cmap_ic_reserve(&m, N); clock_t now = clock(); c_forrange (i, N) { - uint64_t k = cstc64_rand(&rng) & mask; + uint64_t k = crand_next(&rng) & mask; int v = ++cmap_ic_emplace(&m, k, 0).first->second; if (v > 1) printf("%zu: %llx - %d\n", i, k, v); } @@ -33,14 +33,14 @@ using_cvec(x, uint64_t); void distribution(void) { - cstc64_t rng = cstc64_init(seed); // time(NULL), time(NULL)); + crand_t rng = crand_init(seed); // time(NULL), time(NULL)); const size_t N = 1ull << 28, M = 1ull << 9; // 1ull << 10; cmap_x map = cmap_x_with_capacity(M); clock_t now = clock(); - cstc64_uniform_t dist = cstc64_uniform_init(0, M); + crand_uniform_t dist = crand_uniform_init(0, M); c_forrange (N) { - ++cmap_x_emplace(&map, cstc64_uniform(&rng, &dist), 0).first->second; + ++cmap_x_emplace(&map, crand_uniform(&rng, &dist), 0).first->second; } float diff = (float) (clock() - now) / CLOCKS_PER_SEC; diff --git a/examples/ex_gaussian.c b/examples/ex_gaussian.c index 5303dd0c..a3842aae 100644 --- a/examples/ex_gaussian.c +++ b/examples/ex_gaussian.c @@ -25,13 +25,13 @@ int main() // Setup random engine with normal distribution. uint64_t seed = time(NULL); - cstc64_t rng = cstc64_init(seed); - cstc64_normalf_t dist = cstc64_normalf_init(Mean, StdDev); + crand_t rng = crand_init(seed); + crand_normalf_t dist = crand_normalf_init(Mean, StdDev); // Create histogram map cmap_i mhist = cmap_i_init(); for (size_t i = 0; i < N; ++i) { - int index = (int) round( cstc64_normalf(&rng, &dist) ); + int index = (int) round( crand_normalf(&rng, &dist) ); cmap_i_emplace(&mhist, index, 0).first->second += 1; } diff --git a/examples/heap.c b/examples/heap.c index 166dd6ba..83f9d97f 100644 --- a/examples/heap.c +++ b/examples/heap.c @@ -10,15 +10,15 @@ using_cpqueue(f, cvec_f, >); int main() { uint32_t seed = time(NULL); - cstc64_t rng; + crand_t rng; int N = 3000000, M = 100; cpqueue_f pq = cpqueue_f_init(); - rng = cstc64_init(seed); + rng = crand_init(seed); clock_t start = clock(); c_forrange (i, int, N) - cvec_f_push_back(&pq, (float) cstc64_randf(&rng)*100000); + cvec_f_push_back(&pq, (float) crand_nextf(&rng)*100000); cpqueue_f_make_heap(&pq); printf("Built priority queue: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC); @@ -35,7 +35,7 @@ int main() start = clock(); c_forrange (i, int, N) - cpqueue_f_push(&pq, (float) cstc64_randf(&rng)*100000); + cpqueue_f_push(&pq, (float) crand_nextf(&rng)*100000); printf("pushed PQ: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC); c_forrange (i, int, M) { diff --git a/examples/list.c b/examples/list.c index fbfa8830..4f6d16e1 100644 --- a/examples/list.c +++ b/examples/list.c @@ -9,11 +9,11 @@ int main() { const int n = 2000000; clist_fx list = clist_inits; - cstc64_t eng = cstc64_init(1234); - cstc64_uniformf_t dist = cstc64_uniformf_init(100.0f, n); + crand_t rng = crand_init(1234); + crand_uniformf_t dist = crand_uniformf_init(100.0f, n); int m = 0; c_forrange (i, int, n) - clist_fx_push_back(&list, cstc64_uniformf(&eng, &dist)), ++m; + clist_fx_push_back(&list, crand_uniformf(&rng, &dist)), ++m; double sum = 0.0; printf("sumarize %d:\n", m); c_foreach (i, clist_fx, list) diff --git a/examples/priority.c b/examples/priority.c index e81cd8a3..f6b4ecf0 100644 --- a/examples/priority.c +++ b/examples/priority.c @@ -11,19 +11,19 @@ using_cpqueue(i, cvec_i, >); // min-heap (increasing values) int main() { size_t N = 10000000; - cstc64_t pcg = cstc64_init(time(NULL)); - cstc64_uniform_t dist = cstc64_uniform_init(0, N * 10); + crand_t rng = crand_init(time(NULL)); + crand_uniform_t dist = crand_uniform_init(0, N * 10); cpqueue_i heap = cpqueue_i_init(); // Push ten million random numbers to priority queue c_forrange (N) - cpqueue_i_push(&heap, cstc64_uniform(&pcg, &dist)); + cpqueue_i_push(&heap, crand_uniform(&rng, &dist)); // push some negative numbers too. c_push_items(&heap, cpqueue_i, {-231, -32, -873, -4, -343}); c_forrange (N) - cpqueue_i_push(&heap, cstc64_uniform(&pcg, &dist)); + cpqueue_i_push(&heap, crand_uniform(&rng, &dist)); // Extract the hundred smallest. diff --git a/examples/queue.c b/examples/queue.c index 0ff29736..57362493 100644 --- a/examples/queue.c +++ b/examples/queue.c @@ -7,20 +7,20 @@ using_cqueue(i, clist_i); // min-heap (increasing values) int main() { int n = 10000000; - cstc64_uniform_t dist; - cstc64_t rng = cstc64_init(1234); - dist = cstc64_uniform_init(0, n); + crand_uniform_t dist; + crand_t rng = crand_init(1234); + dist = crand_uniform_init(0, n); cqueue_i queue = cqueue_i_init(); // Push ten million random numbers onto the queue. c_forrange (n) - cqueue_i_push(&queue, cstc64_uniform(&rng, &dist)); + cqueue_i_push(&queue, crand_uniform(&rng, &dist)); // Push or pop on the queue ten million times printf("%d\n", n); c_forrange (n) { // range uses initial n only. - int r = cstc64_uniform(&rng, &dist); + int r = crand_uniform(&rng, &dist); if (r & 1) ++n, cqueue_i_push(&queue, r); else diff --git a/examples/random.c b/examples/random.c index 0cf9da6f..de1fe778 100644 --- a/examples/random.c +++ b/examples/random.c @@ -7,18 +7,18 @@ int main() { enum {R = 30}; - const size_t N = 500000000; - uint64_t seed = time(NULL); - cstc64_t stc = cstc64_init(seed); + const size_t N = 1000000000; + uint64_t seed = 1234; // time(NULL); + crand_t rng = crand_init(seed); uint64_t sum = 0; - cstc64_normalf_t dist2 = cstc64_normalf_init(R / 2.0, R / 6.0); + crand_normalf_t dist2 = crand_normalf_init(R / 2.0, R / 6.0); size_t N2 = 10000000; int hist[R] = {0}; sum = 0; c_forrange (N2) { - int n = round((cstc64_normalf(&stc, &dist2) + 0.5)); + int n = round((crand_normalf(&rng, &dist2) + 0.5)); sum += n; if (n >= 0 && n < R) ++hist[n]; } @@ -33,16 +33,16 @@ int main() sum = 0; before = clock(); c_forrange (N) { - sum += cstc64_rand(&stc); + sum += crand_next(&rng); } diff = clock() - before; printf("random : %f secs, %zu %f\n", (float) diff / CLOCKS_PER_SEC, N, (double) sum / N); - cstc64_uniform_t dist1 = cstc64_uniform_init(0, 1000); + crand_uniform_t dist1 = crand_uniform_init(0, 1000); sum = 0; before = clock(); c_forrange (N) { - sum += cstc64_uniform(&stc, &dist1); + sum += crand_uniform(&rng, &dist1); } diff = clock() - before; printf("uniform: %f secs, %zu %f\n", (float) diff / CLOCKS_PER_SEC, N, (double) sum / N); diff --git a/examples/rngtest.c b/examples/rngtest.c index 76666484..db7a8093 100644 --- a/examples/rngtest.c +++ b/examples/rngtest.c @@ -13,14 +13,14 @@ int main(void) clock_t diff, before; uint64_t v; - cstc64_t stc = cstc64_init(time(NULL)); - cstc64_uniform_t idist = cstc64_uniform_init(10, 20); - cstc64_uniformf_t fdist = cstc64_uniformf_init(10, 20); + crand_t rng = crand_init(time(NULL)); + crand_uniform_t idist = crand_uniform_init(10, 20); + crand_uniformf_t fdist = crand_uniformf_init(10, 20); before = clock(); v = 0; c_forrange (NN) { - v += cstc64_rand(&stc); + v += crand_next(&rng); } diff = clock() - before; printf("stc64_rand: %.02f, %zu\n", (float) diff / CLOCKS_PER_SEC, v); @@ -28,13 +28,13 @@ int main(void) before = clock(); v = 0; c_forrange (NN) { - v += cstc64_uniform(&stc, &idist); + v += crand_uniform(&rng, &idist); } diff = clock() - before; printf("stc64_uniform: %.02f, %zu\n\n", (float) diff / CLOCKS_PER_SEC, v); - c_forrange (30) printf("%02zd ", cstc64_uniform(&stc, &idist)); + c_forrange (30) printf("%02zd ", crand_uniform(&rng, &idist)); puts(""); - c_forrange (8) printf("%f ", cstc64_uniformf(&stc, &fdist)); + c_forrange (8) printf("%f ", crand_uniformf(&rng, &fdist)); puts(""); } \ No newline at end of file -- cgit v1.2.3