diff options
| author | tylov <[email protected]> | 2023-07-20 15:09:10 +0200 |
|---|---|---|
| committer | tylov <[email protected]> | 2023-07-20 15:12:29 +0200 |
| commit | 900295256d825fc323149cd223c49787f32a3696 (patch) | |
| tree | 6c79cf4209e3975bb6865e2940b9cb56ea469c73 /misc/examples/algorithms/random.c | |
| parent | 224a04f7fa7549ed94d2a1415eb25829e39a7cca (diff) | |
| download | STC-modified-900295256d825fc323149cd223c49787f32a3696.tar.gz STC-modified-900295256d825fc323149cd223c49787f32a3696.zip | |
Moved examples to sub-directories. Added cotask1.c cotask2.c examples.
Diffstat (limited to 'misc/examples/algorithms/random.c')
| -rw-r--r-- | misc/examples/algorithms/random.c | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/misc/examples/algorithms/random.c b/misc/examples/algorithms/random.c new file mode 100644 index 00000000..b7c0f277 --- /dev/null +++ b/misc/examples/algorithms/random.c @@ -0,0 +1,45 @@ +#include <stdio.h> +#include <time.h> +#include <stc/crand.h> + +int main(void) +{ + const int N = 1000000000; + const uint64_t seed = (uint64_t)time(NULL), range = 1000000; + crand_t rng = crand_init(seed); + + int64_t sum; + clock_t diff, before; + + printf("Compare speed of full and unbiased ranged random numbers...\n"); + sum = 0; + before = clock(); + c_forrange (N) { + sum += (uint32_t)crand_u64(&rng); + } + diff = clock() - before; + printf("full range\t\t: %f secs, %d, avg: %f\n", + (double)diff/CLOCKS_PER_SEC, N, (double)sum/N); + + crand_unif_t dist1 = crand_unif_init(0, range); + rng = crand_init(seed); + sum = 0; + before = clock(); + c_forrange (N) { + sum += crand_unif(&rng, &dist1); // unbiased + } + diff = clock() - before; + printf("unbiased 0-%" PRIu64 "\t: %f secs, %d, avg: %f\n", + range, (double)diff/CLOCKS_PER_SEC, N, (double)sum/N); + + sum = 0; + rng = crand_init(seed); + before = clock(); + c_forrange (N) { + sum += (int64_t)(crand_u64(&rng) % (range + 1)); // biased + } + diff = clock() - before; + printf("biased 0-%" PRIu64 " \t: %f secs, %d, avg: %f\n", + range, (double)diff/CLOCKS_PER_SEC, N, (double)sum/N); + +} |
