Re: [R] Sum(Random Numbers)=100

From: <Bill.Venables_at_csiro.au>
Date: Tue, 08 Jul 2008 18:08:00 +1000

> x <- rmultinom(1, 100, rep(1/50, 50))
> as.vector(x)

 [1] 4 0 5 4 2 2 4 4 3 2 4 1 1 1 2 0 0 0 2 1 0 4 3 3 2 4 2 2 2 0 1 1 4 2 2 2 0 1 1 1 2 3 2 2 4 1 [47] 3 1 3 0
> sum(x)

[1] 100

Bill Venables
CSIRO Laboratories
PO Box 120, Cleveland, 4163
AUSTRALIA

Office Phone (email preferred): +61 7 3826 7251
Fax (if absolutely necessary):  +61 7 3826 7304
Mobile:                         +61 4 8819 4402
Home Phone:                     +61 7 3286 7700
mailto:Bill.Venables_at_csiro.au
http://www.cmis.csiro.au/bill.venables/

-----Original Message-----
From: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-project.org] On Behalf Of Daniel Malter Sent: Tuesday, 8 July 2008 5:32 PM
To: 'Shubha Vishwanath Karanth'; r-help_at_stat.math.ethz.ch Subject: Re: [R] Sum(Random Numbers)=100

For some reason, the while-loop I sent did not work. It was running forever although I don't know why. Anyway, using repeat works fast:

repeat{x=rpois(50,2)

      if(sum(x)==100) break
      } 


-------------------------

cuncta stricte discussurus

-----Ursprüngliche Nachricht-----
Von: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-project.org] Im Auftrag von Shubha Vishwanath Karanth
Gesendet: Tuesday, July 08, 2008 1:59 AM An: r-help_at_stat.math.ethz.ch
Betreff: [R] Sum(Random Numbers)=100

Hi R,  

I need to generate 50 random numbers (preferably poisson), such that their sum is equal to 100. How do I do this?    

Thank you,

Shubha  

This e-mail may contain confidential and/or privileged i...{{dropped:13}}



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.

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 - 08:11:25 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 - 08:31:46 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.

list of date sections of archive