[R] R equivilant to RAND_MAX in C

Rajarshi Guha rxg218 at psu.edu
Tue Mar 23 05:42:00 CET 2004


Hello,
  I have some C code that I'm interfacing to R using the .C calling
interface. Currently the C code uses the rand() function from the GNU C
library to generate random numbers. Since I need the random numbers in a
range from 0 to a (where a is an integer) I use the RAND_MAX macro as

(int)(rand() * (float)(*nobs-1) / (RAND_MAX+1.0))

(taken from the rand() manpage)

However, since I have access to the R RNG's I'd like to use them.

Firstly, would it be an improvement to use the R RNG over that supplied
by the C library?

Secondly, I dont see any mention of an equivilant to RAND_MAX in the
documentation of runif() (as I want to use unif_rand() in my C code).

Is it valid to use the C libraries' RAND_MAX as the maximum value of the
RNG or am I missing something?

As far as I understand using the .C interface I can't call R functions
from my C code (which means I cant access runif()) - is this correct?
As I would rather stay with the .C interface rather than the .Call
interface is there a way to get random numbers within a given range?

Thanks,

-------------------------------------------------------------------
Rajarshi Guha <rxg218 at psu.edu> <http://jijo.cjb.net>
GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE
-------------------------------------------------------------------
All laws are simulations of reality.
-- John C. Lilly




More information about the R-help mailing list