<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
  </head>
  <body text="#000000" bgcolor="#ffffff">
    Hi guys, <br>
    <br>
    I want to produce discrete uniform variates within C++ from the
    runif() function available through Rcpp Sugar. However, I don't seem
    to be able to find the appropriate cast in order to return an
    IntegerVector as opposed to a numeric one. I have naively used
    int(), but this is clearly not sufficient. <br>
    <br>
    Any help would be greatly appreciated,<br>
    Ced<br>
    <br>
    ##########################################<br>
    #### Discrete uniform variate. <br>
    src &lt;- '<br>
    &nbsp; using namespace Rcpp ;<br>
    &nbsp; RNGScope scope;<br>
    &nbsp; int n = as&lt;int&gt;(xn); <br>
    &nbsp; int a = as&lt;int&gt;(xa); <br>
    &nbsp; int b = as&lt;int&gt;(xb); <br>
    &nbsp; IntegerVector vec(n); <br>
    &nbsp; for(int i=0; i&lt;n; ++i) vec[i] = (int)floor(runif(i,a,b)); <br>
    return vec;'<br>
    cxxfun &lt;-
cxxfunction(sig=signature(xn="numeric",xa="numeric",xb="numeric"),body=src,plugin="Rcpp",verbose=TRUE)<br>
    cxxfun(10,0,10);<br>
    <br>
    ##########################<br>
    Error in compileCode(f, code, language = language, verbose =
    verbose) : <br>
    &nbsp; Compilation ERROR, function(s)/method(s) not created!
    file52d217df.cpp: In function &#8216;SEXPREC* file52d217df(SEXPREC*,
    SEXPREC*, SEXPREC*)&#8217;:<br>
    file52d217df.cpp:37: error: invalid cast from type
    &#8216;Rcpp::sugar::SugarMath_1&lt;true, double, double,
    Rcpp::Vector&lt;14&gt;, double (*)(double)&gt;&#8217; to type &#8216;int&#8217; make:
    *** [file52d217df.o] Error 1<br>
    ##################################################<br>
    <br>
    <br>
    <div class="moz-signature">-- <br>
      <div class="moz-signature"><font face="Times" size="3">
          Cedric Ginestet <br>
          Centre for Neuroimaging Sciences (L3.04) <br>
          NIHR Biomedical Research Centre <br>
          Department of Neuroimaging <br>
          Institute of Psychiatry, Box P089 <br>
          King's College London <br>
          De Crespigny Park<br>
          London <br>
          SE5 8AF <br>
        </font>
      </div>
    </div>
  </body>
</html>