Skip to content

Instantly share code, notes, and snippets.

@PhDP
Forked from isag/random integer
Created March 30, 2012 13:02
Show Gist options
  • Save PhDP/2251362 to your computer and use it in GitHub Desktop.
Save PhDP/2251362 to your computer and use it in GitHub Desktop.
Generate integers in the semi-closed range with gsl
// 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