[R] using "rbinom" in C code gives me erroneous results... random variable is not random (always zero)...

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Jun 29 13:00:34 CEST 2006


Your text has lost almost all the carriage returns: please do as we ask 
and not send HTML code.

You appear not to be initializing the random seed in your code: the manual 
says:

   The C code behind @R{}'s @code{r at var{xxx}} functions can be accessed by
   including the header file @file{Rmath.h}; @xref{Distribution
   functions}.  Those calls generate a single variate and should also be
   enclosed in calls to @code{GetRNGstate} and @code{PutRNGstate}.

Why have you not done so?

On Thu, 29 Jun 2006, Vumani Dlamini wrote:

> Dear Listers,
> I am trying to use "rbinom" in my C code, but i always get zeros as output no matter the probability. Am not sure what I am doing wrong because the function has worked before. Attached in an example. Noticed that "rbinom" expects 'n' to be REAL.
> Regards, Vumani
>
> R  2.3.1 (2006-06-01)
> Windows XP
> Gcc
> /* Called this file binom.c and then ran rcmd shlib on it */#include <R.h>#include <Rmath.h>#include <math.h>#include <Rdefines.h>SEXP binomial(SEXP r, SEXP n, SEXP p){ int i; SEXP out; PROTECT(out = allocVector(REALSXP,INTEGER(r)[0]));  for(i = 0; i < INTEGER(r)[0]; ++i){  REAL(out)[i] = rbinom(REAL(n)[0],REAL(p)[0]); } Rprintf("%d   %f   %f\n",INTEGER(r)[0],REAL(n)[0],REAL(p)[0]); UNPROTECT(1); return(out);}
>
> ### used theses line in R
> dyn.load("binom.dll").Call("binomial",as.integer(10),as.double(1),as.double(0.4))dyn.unload("binom.dll")
> _________________________________________________________________
> Try Live.com - your fast, personalized homepage with all the things you care about in one place.
> http://www.live.com/getstarted
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list