From Ancelet at engref.fr  Fri Jan 20 16:45:01 2006
From: Ancelet at engref.fr (Sophie Ancelet)
Date: Fri, 20 Jan 2006 16:45:01 +0100
Subject: [Rd] Troubles with the function rmultinom.c of the R's Random
 Number Generator
Message-ID: <3.0.6.32.20060120164501.007c4c90@Tilia.engref.fr>


Hi,

I'm simulating a Markov chain in Fortran interfaced with R-2.2.1 in order
to generate data according to a Markov Random Field called the Potts model. 

R Version: 
platform i686-pc-linux-gnu
arch     i686
os       linux-gnu
system   i686, linux-gnu
status
major    2
minor    2.1
year     2005
month    12
day      20
svn rev  36812




Each loop of my Fortran calls the function rmultinom.c of the R's Random
Number Generator through the wrapper:

#include <R.h>
#include <Rmath.h>
void F77_SUB(sarmultinom)(int n,
                          double* prob,
                          int K,
                          int* rN){
 rmultinom(n, prob, K, rN);}



My fortran program is:

subroutine testsarmultinom(n,prob,K,rN)
implicit none
integer n,K,rN(K)
double precision prob(K)

call rndstart()
call sarmultinom(n,prob,K,rN)
call rndend()
end


In order to understand better how the function rmultinom.c works, I have
written an R code which calls this fortran subroutine as follows:

system("R CMD SHLIB test-multinom.f wrapper.c")
dyn.load("~/Package/test/test-multinom.so")

n=1
prob=c(0.6,0.1,0.3)
K=3
rN=c(1,0,0)
res<- .Fortran("testsarmultinom",
               as.integer(n),
               as.double(prob),
               as.integer(K),
               as.integer(rN))


Unfortunately, I have some trouble with the results. First, this command
always returns 0 values. In other words, I always get:

>res[[4]]
[1] 0 0 0


Moreover, if I run this R code a second time, an error message appears:
Segmentation fault.

Has somebody ever used rmultinom.c and encountered these problems? My code
must be wrong but I don't know where. In this case, what is the correct way
to call the C function rmultinom.c?

Thanks in advance,

Sophie.




Sophie Ancelet
ENGREF
Laboratoire Grese 
19 avenue du Maine
75 732 Paris Cedex 15 
France
tel: 33 1 45 49 89 27



[Rd] Troubles with the function rmultinom.c of the R's Random Number Generator
Sophie Ancelet