[R] Problem using C random generator called from R

Douglas Bates bates at stat.wisc.edu
Wed Sep 24 18:27:48 CEST 2003


You need to declare the drand48 function within your C source file.
Either 

#include <stdlib.h>

or

extern double drand48(void);


sebastien.dejean at math.ups-tlse.fr writes:

> 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
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help

-- 
Douglas Bates                            bates at stat.wisc.edu
Statistics Department                    608/262-2598
University of Wisconsin - Madison        http://www.stat.wisc.edu/~bates/




More information about the R-help mailing list