# [R] RES: Sum(Random Numbers)=100

From: Leandro Marino <leandro_at_cesgranrio.org.br>
Date: Tue, 08 Jul 2008 09:18:11 -0300

One of possible solutions is:

generate all the numbers.

num <- rpois(...)
num <- round(num/sum(num)*100,0)

sum(num)

I don't know if it is the best solution, but is one!

Leandro Marino

-----Mensagem original-----
De: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-project.org]Em nome de Peter Dalgaard
Enviada em: ter็a-feira, 8 de julho de 2008 05:12 Para: Richard Pearson
Cc: r-help_at_stat.math.ethz.ch
Assunto: Re: [R] Sum(Random Numbers)=100

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

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
```
Received on Tue 08 Jul 2008 - 12:28:07 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 08 Jul 2008 - 13:31:10 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.