R-alpha: unif_rand() again

Peter Dalgaard BSA p.dalgaard@kubism.ku.dk
03 Jun 1997 19:21:57 +0200


Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at> writes:

> I did.  I hoped that something like
> 
> static char *func = "runif";
> 
> double unif_rand() {
...
> 
> might work, but it does not ...

Here's a model that does (seem to) work:

> dyn.load("runif.so");.C("select_rand",runif); .C("unif_rand",3.3)
[[1]]
function (n, min = 0, max = 1) 
.Internal(runif(n, min, max))

[[1]]
[1] 0.126975

> .C("unif_rand",3.3) # the 3.3 is just an arbitrary double...
[[1]]
[1] 0.9233792

> .C("unif_rand",3.3)
[[1]]
[1] 0.03410953

> 

with

runif.c:

-------------------------

static void * func;

double select_rand(void * f)
{
        func = f;
}

static long one = 1L, zero = 0L;
unif_rand(double *x) {
  void *in[3];
  char *modes[] = { "integer", "integer", "integer" };
  long lengths[] = { 1, 1, 1 };  
  double result;
  double *values[1];

  in[0] = (void *)&one;
  in[1] = (void *)&zero;
  in[2] = (void *)&one;  
  call_S((void *)func, 3L, in, modes, lengths, 0L, 1L, values);
  x[0] = values[0][0];
}

--------------------------


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)             FAX: (+45) 35327907
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-