Skip to content
/ Ezra Public

Simple C library to generate uniformally-distributed pseudo-random numbers with no third party dependencies

License

Notifications You must be signed in to change notification settings

fDero/Ezra

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ezra: Easy Randomness

Ezra is a simple C library to generate uniformally-distributed pseudo-random numbers with no third party dependencies.

Understand the API

The API is simple, and revolves around the ezra_random_number_t type defined in <ezra/random.h>.

You can generate a ezra_random_number_t using a generator of your choice, and you can extract either a double or an int32_t from it in a given range.

For instance, assume you want to use an mt19937 generator (Mersenne / Twister generator), you have to setup the generator first:

ezra_mt19937_t mt;
uint64_t seed = time(NULL);
ezra_init_mt19937(seed, &mt);

Then you can use the generator to generate an ezra_random_number_t.

ezra_random_number_t rand_num = ezra_rand_generate_mt19937(&mt);

Once you have it, you can use it to extract a number either using an integer-based range, like so:

ezra_int_range_t range;
ezra_init_int_range(0, 9, &range); // range [0, 9]
int64_t result = ezra_scale_in_int_range(&range, &rand_num);

Or a double, using a real-based range:

ezra_real_range_t range;
ezra_init_open_real_range(0.0, 1.0, &range); // range (0, 1)
double result = ezra_scale_in_real_range(&range, &rand_num);