summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2020-12-14 23:00:15 +0100
committerTyge Løvset <[email protected]>2020-12-14 23:00:15 +0100
commita51861d02f9d8ed8c69fd243de18960307b0fd63 (patch)
tree0684859662ced33942942045652f45f65a07ee35 /examples
parentf08c1bf0fdf00039f9431677a4038c03696a5e01 (diff)
downloadSTC-modified-a51861d02f9d8ed8c69fd243de18960307b0fd63.tar.gz
STC-modified-a51861d02f9d8ed8c69fd243de18960307b0fd63.zip
Reworked the crandom.h module. Changed API (sorry!), made uniform distr. unbiased. Removed 32-bit pcg32 PRNG.
Diffstat (limited to 'examples')
-rw-r--r--examples/benchmark.cpp6
-rw-r--r--examples/birthday.c10
-rw-r--r--examples/ex_gaussian.c6
-rw-r--r--examples/heap.c9
-rw-r--r--examples/list.c6
-rw-r--r--examples/priority.c8
-rw-r--r--examples/queue.c10
-rw-r--r--examples/random.c56
-rw-r--r--examples/rngtest.c44
9 files changed, 58 insertions, 97 deletions
diff --git a/examples/benchmark.cpp b/examples/benchmark.cpp
index ca744e4d..4830c9ba 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;
-crand_rng64_t rng;
-#define SEED(s) rng = crand_rng64_init(seed)
-#define RAND(N) (crand_i64(&rng) & ((1 << N) - 1))
+cstc64_t rng;
+#define SEED(s) rng = cstc64_init(seed)
+#define RAND(N) (cstc64_rand(&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 54ffa30e..fedb2c5c 100644
--- a/examples/birthday.c
+++ b/examples/birthday.c
@@ -14,12 +14,12 @@ const static uint64_t mask = (1ull << 52) - 1;
void repeats(void)
{
- crand_rng64_t rng = crand_rng64_init(seed);
+ cstc64_t rng = cstc64_init(seed);
cmap_ic m = cmap_ic_init();
cmap_ic_reserve(&m, N);
clock_t now = clock();
c_forrange (i, N) {
- uint64_t k = crand_i64(&rng) & mask;
+ uint64_t k = cstc64_rand(&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)
{
- crand_rng32_t rng = crand_rng32_init(seed); // time(NULL), time(NULL));
+ cstc64_t rng = cstc64_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();
- crand_uniform_i32_t dist = crand_uniform_i32_init(0, M);
+ cstc64_uniform_t dist = cstc64_uniform_init(0, M);
c_forrange (N) {
- ++cmap_x_emplace(&map, crand_uniform_i32(&rng, &dist), 0).first->second;
+ ++cmap_x_emplace(&map, cstc64_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 3ed30cf2..5303dd0c 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);
- crand_rng64_t rng = crand_rng64_init(seed);
- crand_normal_f64_t dist = crand_normal_f64_init(Mean, StdDev);
+ cstc64_t rng = cstc64_init(seed);
+ cstc64_normalf_t dist = cstc64_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( crand_normal_f64(&rng, &dist) );
+ int index = (int) round( cstc64_normalf(&rng, &dist) );
cmap_i_emplace(&mhist, index, 0).first->second += 1;
}
diff --git a/examples/heap.c b/examples/heap.c
index 873077fe..166dd6ba 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);
- crand_rng32_t pcg;
+ cstc64_t rng;
int N = 3000000, M = 100;
cpqueue_f pq = cpqueue_f_init();
- pcg = crand_rng32_init(seed);
+ rng = cstc64_init(seed);
clock_t start = clock();
c_forrange (i, int, N)
- cvec_f_push_back(&pq, (float) crand_f32(&pcg)*100000);
+ cvec_f_push_back(&pq, (float) cstc64_randf(&rng)*100000);
cpqueue_f_make_heap(&pq);
printf("Built priority queue: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC);
@@ -33,10 +33,9 @@ int main()
cpqueue_f_pop(&pq);
printf("\n\npopped PQ: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC);
- pcg = crand_rng32_init(seed);
start = clock();
c_forrange (i, int, N)
- cpqueue_f_push(&pq, (float) crand_f32(&pcg)*100000);
+ cpqueue_f_push(&pq, (float) cstc64_randf(&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 4758b072..fbfa8830 100644
--- a/examples/list.c
+++ b/examples/list.c
@@ -9,11 +9,11 @@ int main() {
const int n = 2000000;
clist_fx list = clist_inits;
- crand_rng64_t eng = crand_rng64_init(1234);
- crand_uniform_f64_t dist = crand_uniform_f64_init(100.0f, n);
+ cstc64_t eng = cstc64_init(1234);
+ cstc64_uniformf_t dist = cstc64_uniformf_init(100.0f, n);
int m = 0;
c_forrange (i, int, n)
- clist_fx_push_back(&list, crand_uniform_f64(&eng, &dist)), ++m;
+ clist_fx_push_back(&list, cstc64_uniformf(&eng, &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 e4a20da9..e81cd8a3 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;
- crand_rng64_t pcg = crand_rng64_init(time(NULL));
- crand_uniform_i64_t dist = crand_uniform_i64_init(0, N * 10);
+ cstc64_t pcg = cstc64_init(time(NULL));
+ cstc64_uniform_t dist = cstc64_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, crand_uniform_i64(&pcg, &dist));
+ cpqueue_i_push(&heap, cstc64_uniform(&pcg, &dist));
// push some negative numbers too.
c_push_items(&heap, cpqueue_i, {-231, -32, -873, -4, -343});
c_forrange (N)
- cpqueue_i_push(&heap, crand_uniform_i64(&pcg, &dist));
+ cpqueue_i_push(&heap, cstc64_uniform(&pcg, &dist));
// Extract the hundred smallest.
diff --git a/examples/queue.c b/examples/queue.c
index c56214a7..0ff29736 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;
- crand_uniform_i32_t dist;
- crand_rng32_t rng = crand_rng32_init(1234);
- dist = crand_uniform_i32_init(0, n);
+ cstc64_uniform_t dist;
+ cstc64_t rng = cstc64_init(1234);
+ dist = cstc64_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, crand_uniform_i32(&rng, &dist));
+ cqueue_i_push(&queue, cstc64_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 = crand_uniform_i32(&rng, &dist);
+ int r = cstc64_uniform(&rng, &dist);
if (r & 1)
++n, cqueue_i_push(&queue, r);
else
diff --git a/examples/random.c b/examples/random.c
index 7f1be3f5..4834a974 100644
--- a/examples/random.c
+++ b/examples/random.c
@@ -6,62 +6,38 @@
int main()
{
enum {R = 30};
- const size_t N = 1000000000;
- clock_t difference, before;
+ const size_t N = 100000000;
+ clock_t diff, before;
uint64_t sum = 0;
-
uint64_t seed = time(NULL);
- crand_rng32_t pcg = crand_rng32_init(seed);
- uint32_t range = crand_i32(&pcg) & ((1u << 28) - 1);
- crand_uniform_i32_t dist0 = crand_uniform_i32_init(0, range);
-
- printf("32 uniform: %u\n", dist0.range);
- double fsum = 0;
- before = clock();
- c_forrange (N) {
- fsum += (double) crand_uniform_i32(&pcg, &dist0) / dist0.range;
- }
- difference = clock() - before;
- printf("%zu %f: %f secs\n", N, fsum / N, (float) difference / CLOCKS_PER_SEC);
-
- pcg = crand_rng32_init(seed);
- dist0 = crand_uniform_i32_init(0, range);
- puts("32 unbiased");
- fsum = 0;
- before = clock();
- c_forrange (N) {
- fsum += (double) crand_unbiased_i32(&pcg, &dist0) / dist0.range;
- }
- difference = clock() - before;
- printf("%zu %f: %f secs\n", N, fsum / N, (float) difference / CLOCKS_PER_SEC);
- puts("64 uniform");
- crand_rng64_t stc = crand_rng64_init(seed);
- crand_uniform_i64_t dist1 = crand_uniform_i64_init(0, N);
+ cstc64_t stc = cstc64_init(seed);
+ cstc64_uniform_t dist1 = cstc64_uniform_init(0, N);
sum = 0;
before = clock();
c_forrange (N) {
- sum += crand_uniform_i64(&stc, &dist1);
+ sum += cstc64_uniform(&stc, &dist1);
}
- difference = clock() - before;
- printf("%zu %f: %f secs\n", N, (double) sum / N, (float) difference / CLOCKS_PER_SEC);
-
+ diff = clock() - before;
+ printf("uniform: %zu %f: %f secs\n", N, (double) sum / N, (float) diff / CLOCKS_PER_SEC);
- puts("normal distribution");
- crand_normal_f64_t dist2 = crand_normal_f64_init(R / 2.0, R / 6.0);
+ cstc64_normalf_t dist2 = cstc64_normalf_init(R / 2.0, R / 6.0);
size_t N2 = 10000000;
int hist[R] = {0};
sum = 0;
+ before = clock();
c_forrange (N2) {
- int n = (int) (crand_normal_f64(&stc, &dist2) + 0.5);
+ int n = (int) (cstc64_normalf(&stc, &dist2) + 0.5);
sum += n;
if (n >= 0 && n < R) ++hist[n];
}
-
- cstr_t bar = cstr_init();
+ diff = clock() - before;
+ printf("normal : %zu %f: %f secs\n", N, (double) sum / N2, (float) diff / CLOCKS_PER_SEC);
+
+ cstr_t bar = cstr_inits;
c_forrange (i, int, R) {
- cstr_take(&bar, cstr_with_size(hist[i] * 25ull * R / N2, '*'));
- printf("%2d %s\n", i, bar.str);
+ cstr_resize(&bar, hist[i] * 25ull * R / N2, '*');
+ printf("%3d %s\n", i, bar.str);
}
cstr_del(&bar);
} \ No newline at end of file
diff --git a/examples/rngtest.c b/examples/rngtest.c
index df3aa41c..76666484 100644
--- a/examples/rngtest.c
+++ b/examples/rngtest.c
@@ -6,49 +6,35 @@
#endif
-#define NN 3000000000
+#define NN 1000000000
int main(void)
{
- clock_t difference, before;
+ clock_t diff, before;
uint64_t v;
- crand_rng64_t stc = crand_rng64_init(time(NULL));
- crand_uniform_i64_t idist = crand_uniform_i64_init(10, 20);
- crand_uniform_f64_t fdist = crand_uniform_f64_init(10, 20);
+ 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);
- c_forrange (30) printf("%02zd ", crand_uniform_i64(&stc, &idist));
- puts("");
-
- crand_rng32_t pcg = crand_rng32_init(time(NULL));
- crand_uniform_i32_t i32dist = crand_uniform_i32_init(10, 20);
- crand_uniform_f32_t f32dist = crand_uniform_f32_init(10, 20);
-
- before = clock(); \
+ before = clock();
v = 0;
c_forrange (NN) {
- //v += crand_i32(&pcg);
- v += crand_uniform_i32(&pcg, &i32dist);
+ v += cstc64_rand(&stc);
}
- difference = clock() - before;
- printf("pcg32: %.02f, %zu\n", (float) difference / CLOCKS_PER_SEC, v);
+ diff = clock() - before;
+ printf("stc64_rand: %.02f, %zu\n", (float) diff / CLOCKS_PER_SEC, v);
- before = clock(); \
+ before = clock();
v = 0;
c_forrange (NN) {
- //v += crand_i64(&stc) & 0xffffffff;
- v += crand_uniform_i64(&stc, &idist);
+ v += cstc64_uniform(&stc, &idist);
}
- difference = clock() - before;
- printf("stc64: %.02f, %zu\n", (float) difference / CLOCKS_PER_SEC, v);
+ diff = clock() - before;
+ printf("stc64_uniform: %.02f, %zu\n\n", (float) diff / CLOCKS_PER_SEC, v);
- c_forrange (8) printf("%d ", crand_uniform_i32(&pcg, &i32dist));
+ c_forrange (30) printf("%02zd ", cstc64_uniform(&stc, &idist));
puts("");
-
-
- c_forrange (8) printf("%f ", crand_uniform_f32(&pcg, &f32dist));
- puts("");
-
- c_forrange (8) printf("%f ", crand_uniform_f64(&stc, &fdist));
+ c_forrange (8) printf("%f ", cstc64_uniformf(&stc, &fdist));
puts("");
} \ No newline at end of file