-
-
Save PhDP/2251362 to your computer and use it in GitHub Desktop.
Generate integers in the semi-closed range with gsl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// clang -O3 -DHAVE_INLINE rngint.c -o rngint -lgsl -lgslcblas | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <time.h> | |
#include <gsl/gsl_rng.h> | |
#include <gsl/gsl_randist.h> | |
// Get an integer in the [a,b) semi-closed range. | |
#define get_ulong(rng,a,b) (gsl_rng_uniform_int(rng,b-a)+a) | |
#define get_uint(rng,a,b) ((unsigned int)get_ulong(rng,a,b)) | |
#define get_int(rng,a,b) ((int)get_ulong(rng,a,b)) | |
int main() | |
{ | |
gsl_rng *rng = gsl_rng_alloc(gsl_rng_taus2); | |
gsl_rng_set(rng, time(NULL)); | |
int i = 0; | |
for (; i < 1000; ++i) | |
{ | |
printf("%d\n", get_int(rng,6,28)); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment