From 58497293d88109624d4798c56e888bcbab7c4285 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Mon, 18 Apr 2022 23:53:18 +0200 Subject: crandom.h: Renamed *_init(..) functions to *_new(..). Old names are kept but deprecated. --- benchmarks/plotbench/cpque_benchmark.cpp | 4 +-- benchmarks/shootout_hashmaps.cpp | 2 +- docs/cpque_api.md | 4 +-- docs/crandom_api.md | 14 +++++----- examples/birthday.c | 4 +-- examples/ex_gauss1.c | 4 +-- examples/ex_gauss2.c | 4 +-- examples/list.c | 4 +-- examples/new_queue.c | 4 +-- examples/priority.c | 4 +-- examples/queue.c | 4 +-- examples/random.c | 8 +++--- include/stc/alt/clist.h | 2 +- include/stc/cqueue.h | 4 +-- include/stc/crandom.h | 47 ++++++++++++++++---------------- 15 files changed, 57 insertions(+), 56 deletions(-) diff --git a/benchmarks/plotbench/cpque_benchmark.cpp b/benchmarks/plotbench/cpque_benchmark.cpp index 84bbe468..4a9e830a 100644 --- a/benchmarks/plotbench/cpque_benchmark.cpp +++ b/benchmarks/plotbench/cpque_benchmark.cpp @@ -17,7 +17,7 @@ void std_test() int N = 10000000, M = 10; std::priority_queue, std::greater> pq; - rng = stc64_init(seed); + rng = stc64_new(seed); clock_t start = clock(); c_forrange (i, N) pq.push((float) stc64_randf(&rng)*100000); @@ -41,7 +41,7 @@ void stc_test() c_auto (cpque_f, pq) { - rng = stc64_init(seed); + rng = stc64_new(seed); clock_t start = clock(); c_forrange (i, N) cpque_f_push(&pq, (float) stc64_randf(&rng)*100000); diff --git a/benchmarks/shootout_hashmaps.cpp b/benchmarks/shootout_hashmaps.cpp index cd336931..57bd3383 100644 --- a/benchmarks/shootout_hashmaps.cpp +++ b/benchmarks/shootout_hashmaps.cpp @@ -32,7 +32,7 @@ KHASH_MAP_INIT_INT64(ii, int64_t) stc64_t rng; size_t seed; -#define SEED(s) rng = stc64_init(seed) +#define SEED(s) rng = stc64_new(seed) #define RAND(N) (stc64_rand(&rng) & (((uint64_t)1 << N) - 1)) diff --git a/docs/cpque_api.md b/docs/cpque_api.md index c6a0fc50..b4ad48a9 100644 --- a/docs/cpque_api.md +++ b/docs/cpque_api.md @@ -70,8 +70,8 @@ i_val cpque_X_value_clone(i_val value); int main() { size_t N = 10000000; - stc64_t rng = stc64_init(1234); - stc64_uniform_t dist = stc64_uniform_init(0, N * 10); + stc64_t rng = stc64_new(1234); + stc64_uniform_t dist = stc64_uniform_new(0, N * 10); // Declare heap, with defered drop() c_auto (cpque_i, heap) diff --git a/docs/crandom_api.md b/docs/crandom_api.md index 41c4a574..76b2379e 100644 --- a/docs/crandom_api.md +++ b/docs/crandom_api.md @@ -45,18 +45,18 @@ void csrandom(uint64_t seed); // uint64_t crandom(void); // global stc64_rand(rng) double crandomf(void); // global stc64_randf(rng) -stc64_t stc64_init(uint64_t seed); -stc64_t stc64_with_seq(uint64_t seed, uint64_t seq); // init with stream +stc64_t stc64_new(uint64_t seed); // stc64_init(s) is deprecated +stc64_t stc64_with_seq(uint64_t seed, uint64_t seq); // with unique stream uint64_t stc64_rand(stc64_t* rng); // range [0, 2^64 - 1] double stc64_randf(stc64_t* rng); // range [0.0, 1.0) -stc64_uniform_t stc64_uniform_init(int64_t low, int64_t high); // uniform-distribution +stc64_uniform_t stc64_uniform_new(int64_t low, int64_t high); // uniform-distribution int64_t stc64_uniform(stc64_t* rng, stc64_uniform_t* dist); // range [low, high] -stc64_uniformf_t stc64_uniformf_init(double lowf, double highf); +stc64_uniformf_t stc64_uniformf_new(double lowf, double highf); double stc64_uniformf(stc64_t* rng, stc64_uniformf_t* dist); // range [lowf, highf) -stc64_normalf_t stc64_normalf_init(double mean, double stddev); // normal-distribution +stc64_normalf_t stc64_normalf_new(double mean, double stddev); // normal-distribution double stc64_normalf(stc64_t* rng, stc64_normalf_t* dist); ``` ## Types @@ -89,8 +89,8 @@ int main() // 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); + stc64_t rng = stc64_new(seed); + stc64_normalf_t dist = stc64_normalf_new(Mean, StdDev); // Create histogram map csmap_i mhist = csmap_i_init(); diff --git a/examples/birthday.c b/examples/birthday.c index 94438077..7c95f961 100644 --- a/examples/birthday.c +++ b/examples/birthday.c @@ -17,7 +17,7 @@ static void test_repeats(void) const static uint64_t mask = (1ull << BITS) - 1; printf("birthday paradox: value range: 2^%d, testing repeats of 2^%d values\n", BITS, BITS_TEST); - stc64_t rng = stc64_init(seed); + stc64_t rng = stc64_new(seed); c_auto (cmap_ic, m) { cmap_ic_reserve(&m, N); @@ -38,7 +38,7 @@ void test_distribution(void) { enum {BITS = 26}; printf("distribution test: 2^%d values\n", BITS); - stc64_t rng = stc64_init(seed); + stc64_t rng = stc64_new(seed); const size_t N = 1ull << BITS ; c_auto (cmap_x, map) { diff --git a/examples/ex_gauss1.c b/examples/ex_gauss1.c index 63a4b45e..bdcd1bd3 100644 --- a/examples/ex_gauss1.c +++ b/examples/ex_gauss1.c @@ -30,8 +30,8 @@ int main() // 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); + stc64_t rng = stc64_new(seed); + stc64_normalf_t dist = stc64_normalf_new(Mean, StdDev); // Create and init histogram vec and map with defered destructors: c_auto (cvec_pair, histvec) diff --git a/examples/ex_gauss2.c b/examples/ex_gauss2.c index f2f11a4a..9558afd3 100644 --- a/examples/ex_gauss2.c +++ b/examples/ex_gauss2.c @@ -17,8 +17,8 @@ int main() // 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); + stc64_t rng = stc64_new(seed); + stc64_normalf_t dist = stc64_normalf_new(Mean, StdDev); // Create and init histogram map with defered destruct c_auto (csmap_int, mhist) diff --git a/examples/list.c b/examples/list.c index fd923719..b9f56f13 100644 --- a/examples/list.c +++ b/examples/list.c @@ -12,8 +12,8 @@ int main() { c_auto (clist_fx, list) { - stc64_t rng = stc64_init(1234); - stc64_uniformf_t dist = stc64_uniformf_init(100.0f, n); + stc64_t rng = stc64_new(1234); + stc64_uniformf_t dist = stc64_uniformf_new(100.0f, n); int m = 0; c_forrange (i, int, n) clist_fx_push_back(&list, stc64_uniformf(&rng, &dist)), ++m; diff --git a/examples/new_queue.c b/examples/new_queue.c index 1b7c021c..3dde97f5 100644 --- a/examples/new_queue.c +++ b/examples/new_queue.c @@ -21,8 +21,8 @@ int point_cmp(const Point* a, const Point* b) { int main() { int n = 60000000; - stc64_t rng = stc64_init(time(NULL)); - stc64_uniform_t dist = stc64_uniform_init(0, n); + stc64_t rng = stc64_new(time(NULL)); + stc64_uniform_t dist = stc64_uniform_new(0, n); c_auto (cqueue_int, Q) { diff --git a/examples/priority.c b/examples/priority.c index 1767dba8..6e033386 100644 --- a/examples/priority.c +++ b/examples/priority.c @@ -10,8 +10,8 @@ int main() { size_t N = 10000000; - stc64_t rng = stc64_init(time(NULL)); - stc64_uniform_t dist = stc64_uniform_init(0, N * 10); + stc64_t rng = stc64_new(time(NULL)); + stc64_uniform_t dist = stc64_uniform_new(0, N * 10); c_auto (cpque_i, heap) { // Push ten million random numbers to priority queue diff --git a/examples/queue.c b/examples/queue.c index db5f0e05..5f2fa03d 100644 --- a/examples/queue.c +++ b/examples/queue.c @@ -8,8 +8,8 @@ int main() { int n = 100000000; stc64_uniform_t dist; - stc64_t rng = stc64_init(1234); - dist = stc64_uniform_init(0, n); + stc64_t rng = stc64_new(1234); + dist = stc64_uniform_new(0, n); c_auto (cqueue_i, queue) { diff --git a/examples/random.c b/examples/random.c index 59c1eb95..9ffcf0e2 100644 --- a/examples/random.c +++ b/examples/random.c @@ -6,7 +6,7 @@ int main() { const size_t N = 1000000000; const uint64_t seed = time(NULL), range = 1000000; - stc64_t rng = stc64_init(seed); + stc64_t rng = stc64_new(seed); uint64_t sum; clock_t diff, before; @@ -20,8 +20,8 @@ int main() diff = clock() - before; printf("full range\t\t: %f secs, %" PRIuMAX ", avg: %f\n", (float) diff / CLOCKS_PER_SEC, N, (double) sum / N); - stc64_uniform_t dist1 = stc64_uniform_init(0, range); - rng = stc64_init(seed); + stc64_uniform_t dist1 = stc64_uniform_new(0, range); + rng = stc64_new(seed); sum = 0; before = clock(); c_forrange (N) { @@ -31,7 +31,7 @@ int main() printf("unbiased 0-%" PRIuMAX "\t: %f secs, %" PRIuMAX ", avg: %f\n", range, (float) diff / CLOCKS_PER_SEC, N, (double) sum / N); sum = 0; - rng = stc64_init(seed); + rng = stc64_new(seed); before = clock(); c_forrange (N) { sum += stc64_rand(&rng) % (range + 1); // biased diff --git a/include/stc/alt/clist.h b/include/stc/alt/clist.h index 2fdaf427..d16c1c21 100644 --- a/include/stc/alt/clist.h +++ b/include/stc/alt/clist.h @@ -36,7 +36,7 @@ int main() { clist_ix list = clist_ix_init(); - stc64_t rng = stc64_init(12345); + stc64_t rng = stc64_new(12345); int n; for (int i=0; i<1000000; ++i) // one million clist_ix_push_back(&list, stc64_rand(&rng) >> 32); diff --git a/include/stc/cqueue.h b/include/stc/cqueue.h index 18ac6d08..a9a2fbfa 100644 --- a/include/stc/cqueue.h +++ b/include/stc/cqueue.h @@ -30,8 +30,8 @@ int main() { int n = 10000000; - stc64_t rng = stc64_init(1234); - stc64_uniform_t dist = stc64_uniform_init(0, n); + stc64_t rng = stc64_new(1234); + stc64_uniform_t dist = stc64_uniform_new(0, n); c_auto (cqueue_int, Q) { diff --git a/include/stc/crandom.h b/include/stc/crandom.h index e12b9bed..5a68657e 100644 --- a/include/stc/crandom.h +++ b/include/stc/crandom.h @@ -29,10 +29,10 @@ #include "stc/crandom.h" int main() { uint64_t seed = 123456789; - stc64_t rng = stc64_init(seed); - stc64_uniform_t dist1 = stc64_uniform_init(1, 6); - stc64_uniformf_t dist2 = stc64_uniformf_init(1.0, 10.0); - stc64_normalf_t dist3 = stc64_normalf_init(1.0, 10.0); + stc64_t rng = stc64_new(seed); + stc64_uniform_t dist1 = stc64_uniform_new(1, 6); + stc64_uniformf_t dist2 = stc64_uniformf_new(1.0, 10.0); + stc64_normalf_t dist3 = stc64_normalf_new(1.0, 10.0); uint64_t i = stc64_rand(&rng); int64_t iu = stc64_uniform(&rng, &dist1); @@ -59,23 +59,21 @@ typedef struct stc64_normalf { double mean, stddev, next; unsigned has_next; } s * PractRand to multiple TB input. */ -/* Global STC64 PRNGs */ +/* Global stc64 PRNGs */ STC_API void csrandom(uint64_t seed); STC_API uint64_t crandom(void); STC_API double crandomf(void); -STC_INLINE void stc64_srandom(uint64_t seed) { csrandom(seed); } // alias -STC_INLINE uint64_t stc64_random() { return crandom(); } // alias - -/* Init with sequence number */ +/* Init stc64 prng with and without sequence number */ STC_API stc64_t stc64_with_seq(uint64_t seed, uint64_t seq); +STC_INLINE stc64_t stc64_new(uint64_t seed) + { return stc64_with_seq(seed, seed + 0x3504f333d3aa0b37); } /* Unbiased bounded uniform distribution. range [low, high] */ +STC_API stc64_uniform_t stc64_uniform_new(int64_t low, int64_t high); STC_API int64_t stc64_uniform(stc64_t* rng, stc64_uniform_t* dist); -STC_INLINE stc64_t stc64_init(uint64_t seed) - { return stc64_with_seq(seed, seed + 0x3504f333d3aa0b37); } - +/* Main stc64 prng */ STC_INLINE uint64_t stc64_rand(stc64_t* rng) { uint64_t *s = rng->state; enum {LR=24, RS=11, LS=3}; const uint64_t result = (s[0] ^ (s[3] += s[4])) + s[1]; @@ -97,12 +95,12 @@ STC_INLINE double stc64_uniformf(stc64_t* rng, stc64_uniformf_t* dist) { } /* Init uniform distributed float64 RNG, range [low, high). */ -STC_INLINE stc64_uniformf_t stc64_uniformf_init(double low, double high) { +STC_INLINE stc64_uniformf_t stc64_uniformf_new(double low, double high) { return c_make(stc64_uniformf_t){low, high - low}; } /* Marsaglia polar method for gaussian/normal distribution, float64. */ -STC_INLINE stc64_normalf_t stc64_normalf_init(double mean, double stddev) { +STC_INLINE stc64_normalf_t stc64_normalf_new(double mean, double stddev) { return c_make(stc64_normalf_t){mean, stddev, 0.0, 0}; } @@ -121,6 +119,15 @@ STC_INLINE double stc64_normalf(stc64_t* rng, stc64_normalf_t* dist) { return (u1 * m) * dist->stddev + dist->mean; } +/* Following functions are deprecated (will be removed in the future): */ +STC_INLINE void stc64_srandom(uint64_t seed) { csrandom(seed); } +STC_INLINE uint64_t stc64_random() { return crandom(); } +STC_INLINE stc64_t stc64_init(uint64_t seed) { return stc64_new(seed); } +STC_INLINE stc64_uniformf_t stc64_uniformf_init(double low, double high) + { return stc64_uniformf_new(low, high); } +STC_INLINE stc64_normalf_t stc64_normalf_init(double mean, double stddev) + { return stc64_normalf_new(mean, stddev); } + /* -------------------------- IMPLEMENTATION ------------------------- */ #if defined(_i_implement) @@ -132,7 +139,7 @@ static stc64_t stc64_global = {{ }}; STC_DEF void csrandom(uint64_t seed) { - stc64_global = stc64_init(seed); + stc64_global = stc64_new(seed); } STC_DEF uint64_t crandom(void) { @@ -151,18 +158,12 @@ STC_DEF stc64_t stc64_with_seq(uint64_t seed, uint64_t seq) { return rng; } -#ifdef _MSC_VER -#pragma warning(disable: 4146) // unary minus operator applied to unsigned type -#endif /* Init unbiased uniform uint RNG with bounds [low, high] */ -STC_DEF stc64_uniform_t stc64_uniform_init(int64_t low, int64_t high) { +STC_DEF stc64_uniform_t stc64_uniform_new(int64_t low, int64_t high) { stc64_uniform_t dist = {low, (uint64_t) (high - low + 1)}; - dist.threshold = (uint64_t)(-dist.range) % dist.range; + dist.threshold = (uint64_t)-(int64_t)dist.range % dist.range; return dist; } -#ifdef _MSC_VER -#pragma warning(default: 4146) -#endif /* Int uniform distributed RNG, range [low, high]. */ STC_DEF int64_t stc64_uniform(stc64_t* rng, stc64_uniform_t* d) { -- cgit v1.2.3