On Fri, Jul 13, 2012 at 7:55 PM, Chandler Zuo <z...@stat.wisc.edu> wrote:
> Hi,
>
> Has anyone encountered the problem of rgamma function in C? The following
> simplified program always dies for me, and I wonder if anyone can tell me
> the reason.
>
> #include <Rmath.h>
> #include <time.h>
> #include <Rinternals.h>
>
> SEXP generateGamma ()
> {
>     srand(time(NULL));
>     return (rgamma(5000,1));
> }

rgamma doesn't return an SEXP, it returns a double.  Also, the srand()
call is pointless.

> Has anyone encountered a similar problem before? Is there another way of
> generating Gamma random variable in C?
>
> P.S. I have no problem compiling and loading this function in R.

Strange. You should get compiler warnings that the return type is
incompatible.  I get

foo.c: In function ‘generateGamma’:
foo.c:7: warning: implicit declaration of function ‘srand’
foo.c:8: error: incompatible types in return

I thought the ANSI standard actually *required* a diagnostic for the
incompatible return types.

   -thomas

-- 
Thomas Lumley
Professor of Biostatistics
University of Auckland

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to