For now I am using the following, which seems to work.  I didn&#39;t really need all the functionality from sample(); I just needed to get a random index (but with non-uniform probabilities).  It might be cleaner to use std::accumulate in rcategorical, but I got this working first.  Also, rcategorical requires a double[K]; how do I cast my probabilities argument P as a doubles array?  I naively thought P would already be an array of doubles, but rcategorical(P,K) doesn&#39;t compile.  <div>

<br></div><div>Any thoughts/suggestions on how to improve on this first attempt?</div><div>Thanks in advance,</div><div>Chris</div><div><br></div><div>###<br><div><br></div><div><div>inc &lt;-</div><div>&#39;int rcategorical(double probs[],int K) {</div>

<div>  double total = 0;</div><div>  double cumpr = 0;</div><div>  int k = 0;</div><div>  for (k = 0; k &lt; K; k++) {</div><div>    total += probs[k];</div><div>  }</div><div>  double u = unif_rand();</div><div>  for (k = 0; k &lt; K; k++) {</div>

<div>    cumpr += probs[k] / total;</div><div>    if (u &lt; cumpr) break;</div><div>  }</div><div>  return(k);</div><div>}&#39;</div><div><br></div><div>src &lt;- &#39;</div><div>Rcpp:NumericVector probs(P);</div><div>int K = probs.size();</div>

<div>double pr[K];</div><div>for (int k = 0; k &lt; K; k++) {  // copy elements over one-by-one?  Noob status, but works. :-)</div><div>  pr[k] = probs[k];</div><div>}</div><div>int R = rcategorical(pr,K);</div><div>return wrap(R);</div>

<div>&#39;</div><div>fun &lt;-  cxxfunction(signature(P=&quot;numeric&quot;),includes=inc,body = src, plugin=&quot;Rcpp&quot;)</div><div>p &lt;- c(.2,.3,.4,.1)</div><div>fun(p)</div><br><div class="gmail_quote">On Sat, May 14, 2011 at 6:19 PM, Dirk Eddelbuettel <span dir="ltr">&lt;<a href="mailto:edd@debian.org">edd@debian.org</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div class="im"><br>
On 14 May 2011 at 18:01, Chris DuBois wrote:<br>
| Thanks Dirk.  Passing the addition function in the includes argument worked<br>
| great.  <br>
|<br>
| Also, one of the links from your suggested Google search led me to this<br>
| (working) example for std::accumulate:<br>
|<br>
| src &lt;- &#39;NumericVector xx(x);<br>
| return wrap( std::accumulate( xx.begin(), xx.end(), 0.0));&#39;<br>
| fx &lt;- cxxfunction(signature( x = &quot;numeric&quot; ),body=src,plugin = &quot;Rcpp&quot;)<br>
| fx(1:10)  # 55<br>
|<br>
| I had been copying the quickref and using:  std::accumulate( xx.begin(),<br>
| xx.end(),std::plus&lt;double&gt;(),0.0)<br>
|<br>
| I think this might be a typo where the last two arguments are transposed (which<br>
| I shouldn&#39;t have noticed after browsing the function&#39;s c++ reference), as the<br>
| following works just fine:  std::accumulate( xx.begin(), xx.end<br>
| (),0.0,std::plus&lt;double&gt;())<br>
<br>
</div>Good catch.  The arguments are<br>
<br>
     start, end, intial value, (optional) operator function<br>
<br>
and std::plus&lt;double&gt;() is the default for summation. So that was an error,<br>
and hence a Thank You! for catching it.  I just committed this new section:<br>
<br>
<br>
\paragraph{STL interface}~<br>
  \newline<br>
&lt;&lt;lang=cpp&gt;&gt;=<br>
// sum a vector from beginning to end<br>
double s = std::accumulate(x.begin(),<br>
   x.end(), 0.0);<br>
// prod of elements from beginning to end<br>
int p = std::accumulate(vec.begin(),<br>
    vec.end(), 1, std::multiplies&lt;int&gt;());<br>
// inner_product to compute sum of squares<br>
double s2 = std::inner_product(res.begin(),<br>
    res.end(), res.begin(), 0.0);<br>
@<br>
<br>
<br>
which shows std::multiplies&lt;int&gt;() as an alternative operator for accumulate<br>
as well as inner_product for a sum-of-squares computation (as once suggested<br>
by Doug).<br>
<div><div></div><div class="h5"><br>
Dirk<br>
<br>
--<br>
Gauss once played himself in a zero-sum game and won $50.<br>
                      -- #11 at <a href="http://www.gaussfacts.com" target="_blank">http://www.gaussfacts.com</a><br>
</div></div></blockquote></div><br></div></div>