help-gsl
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-gsl] Uniformly distributed int in [0,n-1]


From: Joseph Wakeling
Subject: [Help-gsl] Uniformly distributed int in [0,n-1]
Date: Sat, 17 Dec 2005 20:43:50 +0100
User-agent: Mozilla Thunderbird 1.0.6 (X11/20050715)

Hello all,

As I mentioned in earlier emails I've been playing around a bit with the random number generators in the GSL.

I'm wondering if someone can advise on appropriate ways to generate uniformly-distributed ints in the range [0,n-1]. My traditional way has always been to take a function ranf() which returns a floating-point number in [0,1), and do,

x = (int) (n*ranf());

But, if we suppose I have a function like the rand() function in C that returns an int between 0 and RAND_MAX - 1, would it be appropriate to do,

x = rand()%n;

Or would this unduly affect the uniform distribution?

I note that the gsl_rng_uniform_int function uses a method like this (simplifying slightly...;-):

do { x = rand()/(RAND_MAX/n); } while( x >= n);

Could someone advise? If they are equivalent, what about the speed of these alternatives?

Many thanks,

      -- Joe







reply via email to

[Prev in Thread] Current Thread [Next in Thread]