summaryrefslogtreecommitdiffhomepage
path: root/misc/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-03-30 17:59:08 +0200
committerTyge Løvset <[email protected]>2023-03-30 17:59:08 +0200
commita0a290645828c88597efce80f6b0f5a958cefa89 (patch)
tree53dc78071653b515a06a60baf4488a1b6d080b32 /misc/examples
parent32df5677c9906661e91aad294e45a258e2eaab18 (diff)
downloadSTC-modified-a0a290645828c88597efce80f6b0f5a958cefa89.tar.gz
STC-modified-a0a290645828c88597efce80f6b0f5a958cefa89.zip
Added crand.h - Alternative API to crandom.h, which will be deprecated.
Diffstat (limited to 'misc/examples')
-rw-r--r--misc/examples/birthday.c10
-rw-r--r--misc/examples/gauss2.c10
-rw-r--r--misc/examples/list.c7
-rw-r--r--misc/examples/new_queue.c10
-rw-r--r--misc/examples/priority.c10
-rw-r--r--misc/examples/queue.c12
-rw-r--r--misc/examples/random.c16
7 files changed, 37 insertions, 38 deletions
diff --git a/misc/examples/birthday.c b/misc/examples/birthday.c
index 2b52cc48..c301128a 100644
--- a/misc/examples/birthday.c
+++ b/misc/examples/birthday.c
@@ -1,7 +1,7 @@
#include <math.h>
#include <stdio.h>
#include <time.h>
-#include <stc/crandom.h>
+#include <stc/crand.h>
#define i_tag ic
#define i_key uint64_t
@@ -17,11 +17,11 @@ 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_new(seed);
+ crand_t rng = crand_init(seed);
cmap_ic m = cmap_ic_with_capacity(N);
c_forrange (i, N) {
- uint64_t k = stc64_rand(&rng) & mask;
+ uint64_t k = crand_u64(&rng) & mask;
int v = cmap_ic_insert(&m, k, 0).ref->second += 1;
if (v > 1) printf("repeated value %" PRIu64 " (%d) at 2^%d\n",
k, v, (int)log2((double)i));
@@ -38,12 +38,12 @@ void test_distribution(void)
{
enum {BITS = 26};
printf("distribution test: 2^%d values\n", BITS);
- stc64_t rng = stc64_new(seed);
+ crand_t rng = crand_init(seed);
const size_t N = 1ull << BITS ;
cmap_x map = {0};
c_forrange (N) {
- uint64_t k = stc64_rand(&rng);
+ uint64_t k = crand_u64(&rng);
cmap_x_insert(&map, k & 0xf, 0).ref->second += 1;
}
diff --git a/misc/examples/gauss2.c b/misc/examples/gauss2.c
index 7fede5aa..df709d03 100644
--- a/misc/examples/gauss2.c
+++ b/misc/examples/gauss2.c
@@ -1,7 +1,7 @@
#include <stdio.h>
#include <time.h>
-#include <stc/crandom.h>
+#include <stc/crand.h>
#include <stc/cstr.h>
// Declare int -> int sorted map.
@@ -13,21 +13,21 @@ int main()
{
enum {N = 5000000};
uint64_t seed = (uint64_t)time(NULL);
- stc64_t rng = stc64_new(seed);
- const double Mean = round(stc64_randf(&rng)*98.f - 49.f), StdDev = stc64_randf(&rng)*10.f + 1.f, Scale = 74.f;
+ crand_t rng = crand_init(seed);
+ const double Mean = round(crand_f64(&rng)*98.f - 49.f), StdDev = crand_f64(&rng)*10.f + 1.f, Scale = 74.f;
printf("Demo of gaussian / normal distribution of %d random samples\n", N);
printf("Mean %f, StdDev %f\n", Mean, StdDev);
// Setup random engine with normal distribution.
- stc64_normalf_t dist = stc64_normalf_new(Mean, StdDev);
+ crand_norm_t dist = crand_norm_init(Mean, StdDev);
// Create and init histogram map with defered destruct
csmap_int hist = {0};
cstr bar = {0};
c_forrange (N) {
- int index = (int)round( stc64_normalf(&rng, &dist) );
+ int index = (int)round(crand_norm(&rng, &dist));
csmap_int_insert(&hist, index, 0).ref->second += 1;
}
diff --git a/misc/examples/list.c b/misc/examples/list.c
index 9f0b2504..eb81067d 100644
--- a/misc/examples/list.c
+++ b/misc/examples/list.c
@@ -1,7 +1,7 @@
#include <stdio.h>
#include <time.h>
#include <stc/algo/filter.h>
-#include <stc/crandom.h>
+#include <stc/crand.h>
#define i_type DList
#define i_val double
@@ -11,11 +11,10 @@ int main() {
const int n = 3000000;
DList list = {0};
- stc64_t rng = stc64_new(1234567);
- stc64_uniformf_t dist = stc64_uniformf_new(100.0f, n);
+ crand_t rng = crand_init(1234567);
int m = 0;
c_forrange (n)
- DList_push_back(&list, stc64_uniformf(&rng, &dist)), ++m;
+ DList_push_back(&list, crand_f64(&rng)*n + 100), ++m;
double sum = 0.0;
printf("sumarize %d:\n", m);
diff --git a/misc/examples/new_queue.c b/misc/examples/new_queue.c
index f7887ffb..044e44cb 100644
--- a/misc/examples/new_queue.c
+++ b/misc/examples/new_queue.c
@@ -1,4 +1,4 @@
-#include <stc/crandom.h>
+#include <stc/crand.h>
#include <stc/forward.h>
#include <stdio.h>
#include <time.h>
@@ -22,20 +22,20 @@ int point_cmp(const Point* a, const Point* b) {
int main() {
int n = 50000000;
- stc64_t rng = stc64_new((uint64_t)time(NULL));
- stc64_uniform_t dist = stc64_uniform_new(0, n);
+ crand_t rng = crand_init((uint64_t)time(NULL));
+ crand_unif_t dist = crand_unif_init(0, n);
IQ Q = {0};
// Push 50'000'000 random numbers onto the queue.
c_forrange (n)
- IQ_push(&Q, (int)stc64_uniform(&rng, &dist));
+ IQ_push(&Q, (int)crand_unif(&rng, &dist));
// Push or pop on the queue 50 million times
printf("befor: size %" c_ZI ", capacity %" c_ZI "\n", IQ_size(&Q), IQ_capacity(&Q));
c_forrange (n) {
- int r = (int)stc64_uniform(&rng, &dist);
+ int r = (int)crand_unif(&rng, &dist);
if (r & 3)
IQ_push(&Q, r);
else
diff --git a/misc/examples/priority.c b/misc/examples/priority.c
index d3d0283e..95dd3183 100644
--- a/misc/examples/priority.c
+++ b/misc/examples/priority.c
@@ -1,7 +1,7 @@
#include <stdio.h>
#include <time.h>
-#include <stc/crandom.h>
+#include <stc/crand.h>
#define i_val int64_t
#define i_cmp -c_default_cmp // min-heap (increasing values)
@@ -10,22 +10,22 @@
int main() {
intptr_t N = 10000000;
- stc64_t rng = stc64_new((uint64_t)time(NULL));
- stc64_uniform_t dist = stc64_uniform_new(0, N * 10);
+ crand_t rng = crand_init((uint64_t)time(NULL));
+ crand_unif_t dist = crand_unif_init(0, N * 10);
cpque_i heap = {0};
// Push ten million random numbers to priority queue
printf("Push %" c_ZI " numbers\n", N);
c_forrange (N)
- cpque_i_push(&heap, stc64_uniform(&rng, &dist));
+ cpque_i_push(&heap, crand_unif(&rng, &dist));
// push some negative numbers too.
c_forlist (i, int, {-231, -32, -873, -4, -343})
cpque_i_push(&heap, *i.ref);
c_forrange (N)
- cpque_i_push(&heap, stc64_uniform(&rng, &dist));
+ cpque_i_push(&heap, crand_unif(&rng, &dist));
puts("Extract the hundred smallest.");
c_forrange (100) {
diff --git a/misc/examples/queue.c b/misc/examples/queue.c
index 0f387d52..83c18d09 100644
--- a/misc/examples/queue.c
+++ b/misc/examples/queue.c
@@ -1,4 +1,4 @@
-#include <stc/crandom.h>
+#include <stc/crand.h>
#include <stdio.h>
#define i_val int
@@ -7,20 +7,20 @@
int main() {
int n = 100000000;
- stc64_uniform_t dist;
- stc64_t rng = stc64_new(1234);
- dist = stc64_uniform_new(0, n);
+ crand_unif_t dist;
+ crand_t rng = crand_init(1234);
+ dist = crand_unif_init(0, n);
cqueue_i queue = {0};
// Push ten million random numbers onto the queue.
c_forrange (n)
- cqueue_i_push(&queue, (int)stc64_uniform(&rng, &dist));
+ cqueue_i_push(&queue, (int)crand_unif(&rng, &dist));
// Push or pop on the queue ten million times
printf("%d\n", n);
c_forrange (n) { // forrange uses initial n only.
- int r = (int)stc64_uniform(&rng, &dist);
+ int r = (int)crand_unif(&rng, &dist);
if (r & 1)
++n, cqueue_i_push(&queue, r);
else
diff --git a/misc/examples/random.c b/misc/examples/random.c
index e27279a0..ea9c483e 100644
--- a/misc/examples/random.c
+++ b/misc/examples/random.c
@@ -1,12 +1,12 @@
#include <stdio.h>
#include <time.h>
-#include <stc/crandom.h>
+#include <stc/crand.h>
int main()
{
const size_t N = 1000000000;
const uint64_t seed = (uint64_t)time(NULL), range = 1000000;
- stc64_t rng = stc64_new(seed);
+ crand_t rng = crand_init(seed);
int64_t sum;
clock_t diff, before;
@@ -15,28 +15,28 @@ int main()
sum = 0;
before = clock();
c_forrange (N) {
- sum += (uint32_t)stc64_rand(&rng);
+ sum += (uint32_t)crand_u64(&rng);
}
diff = clock() - before;
printf("full range\t\t: %f secs, %" c_ZI ", avg: %f\n",
(float)diff / CLOCKS_PER_SEC, N, (float)sum / (float)N);
- stc64_uniform_t dist1 = stc64_uniform_new(0, range);
- rng = stc64_new(seed);
+ crand_unif_t dist1 = crand_unif_init(0, range);
+ rng = crand_init(seed);
sum = 0;
before = clock();
c_forrange (N) {
- sum += stc64_uniform(&rng, &dist1); // unbiased
+ sum += crand_unif(&rng, &dist1); // unbiased
}
diff = clock() - before;
printf("unbiased 0-%" PRIu64 "\t: %f secs, %" c_ZI ", avg: %f\n",
range, (float)diff/CLOCKS_PER_SEC, N, (float)sum / (float)N);
sum = 0;
- rng = stc64_new(seed);
+ rng = crand_init(seed);
before = clock();
c_forrange (N) {
- sum += (int64_t)(stc64_rand(&rng) % (range + 1)); // biased
+ sum += (int64_t)(crand_u64(&rng) % (range + 1)); // biased
}
diff = clock() - before;
printf("biased 0-%" PRIu64 " \t: %f secs, %" c_ZI ", avg: %f\n",