[R] Sum(Random Numbers)=100

Peter Dalgaard p.dalgaard at biostat.ku.dk
Tue Jul 8 10:11:36 CEST 2008


Richard Pearson wrote:
> Shubha
>
> Does table(ceiling(runif(100,0,50))) give you something like you want?
That's a neat(-ish) solution of Moshe's multinomial formulation.

More generally, if p is a vector of probabilities you can simulate 
independent indicators with cut(runif(N), c(0,cumsum(p)) and then 
multinomials by tabulation.

However, it is inefficient for large N, and awkward if you want 
replicates, which is why we have rmultinom(). In the present case use 
rmultinom(1, 100, rep(1/50,50))

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907



More information about the R-help mailing list