Sample or Rmultinom (sampling)
Draw one of {1,..,K} with probabilities "probf"
Needs function: sumv
int Rmultinom(Rcpp::NumericVector probs )
{ int Kf = probs.size();
int v;
double x( 1 );
x=runif(1)(0);
Rcpp::NumericVector probsum( Kf ); //cumsum of normalized probs
double sprob=rcpp_sumv(probs); //sum of the probs vector
probs(0)=probs(0)/sprob;
probsum( 0 )= probs( 0 );
for( int k = 1; k < Kf; k++ )
{ probs(k)=probs(k)/sprob; // Normalize the probs vector
probsum(k)=probs(k)+probsum(k-1);
}
if( x < probsum(0) ){ v=0; }
for( int k = 0; k < (Kf-1); k++ )
{ if(x > probsum(k) & x< probsum(k+1)){v=k+1;}
}
if(x > probsum(Kf-1)){v=Kf-1;}
return v+1;
}