[R] Problem using C random generator called from R

sebastien.dejean@math.ups-tlse.fr sebastien.dejean at math.ups-tlse.fr
Wed Sep 24 10:15:51 CEST 2003


Hello

I'm trying to use C random generator function drand48() which return 
floating-point values, uniformly distributed over [0,1], 
(http://www.opengroup.org/onlinepubs/007908799/xsh/drand48.html). When 
values are returned to R, they are not in [0,1]. A simple C program using 
drand48() gives values in [0,1] so I suppose there is a problem (type 
definition ?) between C and R. Here are R and C function and an example.

# R function
simulC <- function(n)
   {
dyn.load("test.so")
simR <- runif(n)
simC <- .C("test",
        as.integer(n),
        res=double(n))$res
out <- list(simR=simR,simC=simC)
out
}

// C function
void test(int *n,double *res)
{
   int i;
   for(i=0;i<*n;i++) {res[i] = drand48();}
}

# Result of x <- simulC(5)
 > x
$simR
  [1] 0.398567942 0.866053345 0.332070718 0.535814830 0.473418784
$simC
  [1] -1222291199   211182456  1007036963  1254056690  -646279915


Any help will be greatly appreciated,
Sébastien



--
Sébastien Déjean ~~~~~~~~~~~
http://www.lsp.ups-tlse.fr/Fp/Dejean




More information about the R-help mailing list