You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
1.8 KiB
73 lines
1.8 KiB
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#define _RNGXX_INLINE_ONLY
|
|
#include <rngxx.h>
|
|
|
|
static int next(rng_t* rng, const int* min, const int* max)
|
|
{
|
|
int output;
|
|
if(rng_next_tyb(rng, &output, RNG_TY_INT32, RNG_TYQ_SIGNED, min, max)) return output;
|
|
|
|
fprintf(stderr, "next<int>() failed\n");
|
|
exit(-1);
|
|
}
|
|
|
|
#define TREF(x) ( (const __typeof(x)[]){ (x) } )
|
|
|
|
int main()
|
|
{
|
|
rng_dyn_ctor_ref rcref = rng_ctor_ref("crand");
|
|
rng_ctor_fn rctor = rng_ctor("crand");
|
|
|
|
rng_t* engine = NULL;
|
|
if(rctor) {
|
|
printf("constructing direct\n");
|
|
engine = rctor(TREF((u64)time(NULL)));
|
|
} else {
|
|
printf("constructing through ref\n");
|
|
engine = rng_ctor_call(rcref, TREF((u64)time(NULL)));
|
|
}
|
|
if(!engine) {
|
|
printf("fallback to `rng_new_named()`\n");
|
|
engine = rng_new_named("crand", TREF((u64)time(NULL))); //rng_new(rng_kind_crand, (const u64[]){ time(null) });
|
|
}
|
|
if(!engine) {
|
|
printf("fallback to static init\n");
|
|
engine = rng_new(RNG_KIND_CRAND, (const u64[]){ time(NULL) });
|
|
}
|
|
if(!engine) {
|
|
fprintf(stderr, "failed to create engine\n");
|
|
return 1;
|
|
}
|
|
printf("%d %d %d\n", next(engine, NULL, TREF(100)), next(engine, TREF(10), TREF(20)), next(engine, NULL, NULL));
|
|
printf("%ld\n", rng_next_i64(engine, NULL, NULL));
|
|
printf("%lf\n", rng_next_f64(engine, TREF(1.0), TREF(100.0)));
|
|
printf("%d\n", rng_next_bool(engine));
|
|
|
|
|
|
rng_bool_t* array = rng_nextam_bool(engine, 100);
|
|
if(!array) {
|
|
fprintf(stderr, "failed to allocate for array\n");
|
|
return 1;
|
|
}
|
|
for(int i=0;i<100;i++)
|
|
printf("%d ", array[i]);
|
|
printf("\n"); free(array);
|
|
|
|
int ints[100];
|
|
if(!rng_nexta_i32(engine, ints, 100, NULL, TREF(100))) {
|
|
fprintf(stderr, "failed to compute int array\n");
|
|
return 1;
|
|
}
|
|
|
|
for(size_t i=0;i<sizeof(ints)/sizeof(ints[0]);i++)
|
|
printf("%d ", ints[i]);
|
|
printf("\n");
|
|
|
|
rng_free(engine);
|
|
return 0;
|
|
}
|