<div dir="ltr">Dear list, <div><br></div><div><br></div><div>I am a newbie to rcpp and currently I am trying to translate some R code into C++ code.</div><div><br></div><div>In particular, when I was trying to translate the following R code into C++ code:</div><div><br></div><div>    <b>Z[i] <- sample(15, 1, replace = FALSE, prob = (exp(prob - deno)))</b></div><div><br></div><div>I found some errors in my C code, which troubles me a lot. </div><div><br></div><div>Let's elaborate a little bit.</div><div><br></div><div>I found that Rcpp does not provide a <i>sample</i> function, so I follow the instructions <a href="http://gallery.rcpp.org/articles/using-the-Rcpp-based-sample-implementation/">here</a> to use </div><div><i>RcppArmadillo::sample.</i><br></div><div><br></div><div>However, even though the first parameter of <i>sample</i> function in R can be a integer:</div><div><br></div><div><div>    function (x, size, replace = FALSE, prob = NULL) </div><div>    {</div><div>      <b>  if (length(x) == 1L && is.numeric(x) && x >= 1) {</b></div><div><b>            if (missing(size)) </b></div><div><b>                size <- x</b></div><div>           <b> <a href="http://sample.int">sample.int</a>(x, size, replace, prob)</b></div><div>        }</div><div>        else {</div><div>            if (missing(size)) </div><div>                size <- length(x)</div><div>            x[<a href="http://sample.int">sample.int</a>(length(x), size, replace, prob)]</div><div>        }</div><div>    }</div></div><div><br></div><div>I figure out that the first parameter of <i>RcppArmadillo::sample </i>cannot be a integer, it only accepts </div><div>a vector. So given my R code, I translate it into the following C code.</div><div><br></div><div>   <b>     Rcpp:NumericVector JJ(15);</b></div><div><b>        JJ[0] = 15;</b></div><div><b>        </b></div><div><b>        Z[i-1] = RcppArmadillo::sample(JJ, 1, false, (exp(prob - deno)))[0];</b></div><div><br></div><div><br></div><div>Then I found some errors in the above code, say, while in the original R code, Z[i] is a random number<b> </b><span style="font-size:12.8000001907349px"><b>between 1 and 15</b>, in the above C code, Z[i-1] is <b>either 0 or 15</b>. </span></div><div><span style="font-size:12.8000001907349px"><br></span></div><div><span style="font-size:12.8000001907349px">Am I clear? Could anyone give me some help on this issue? Thank you a lot!</span></div><div><span style="font-size:12.8000001907349px"><br></span></div><div><span style="font-size:12.8000001907349px"><br></span></div><div><span style="font-size:12.8000001907349px">Shuai</span></div><div><br></div><div><br></div></div>