hi My inputs is min=(10,10,10,10,10) and max=(100,100,100,100,100) total = 300 i have to generate 5 numbers between min and max and those numbers should sum upto total Can anyone help? ----- Thanks in Advance Arun -- View this message in context: http://r.789695.n4.nabble.com/Generating-Random-Numbers-tp4637020.html Sent from the R help mailing list archive at Nabble.com.
Is this homework? runif() is one way to generate random numbers, but there are others depending on the distribution desired. And of course the fifth number is deterministic. Sarah On Thu, Jul 19, 2012 at 7:12 AM, arunkumar1111 <akpbond007 at gmail.com> wrote:> hi > > My inputs is min=(10,10,10,10,10) and max=(100,100,100,100,100) > total = 300 > i have to generate 5 numbers between min and max and those numbers should > sum upto total > > Can anyone help? > > ----- > Thanks in Advance > Arun-- Sarah Goslee http://www.functionaldiversity.org
Hello, Try this: #Generate 4 random numbers. set.seed(1) ?rnorm(4,60) #[1] 59.37355 60.18364 59.16437 61.59528> sum(rnorm(4,60))#[1] 240.7348 ?fifthnumber<-300-240.7348 ?fifthnumber #[1] 59.2652 A.K. ----- Original Message ----- From: arunkumar1111 <akpbond007 at gmail.com> To: r-help at r-project.org Cc: Sent: Thursday, July 19, 2012 7:12 AM Subject: [R] Generating Random Numbers hi My inputs is min=(10,10,10,10,10) and max=(100,100,100,100,100) total = 300 i have to generate 5 numbers between min and max and those numbers should sum upto total Can anyone help? ----- Thanks in Advance ? ? ? ? Arun -- View this message in context: http://r.789695.n4.nabble.com/Generating-Random-Numbers-tp4637020.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.
Those restrictions you have given do not define a unique distribution! so you need to think better about what you need. For instance, if you want a uniform distribution between min and max with n=5 independent observations from that, but conditional upon sum=total. For that, you could use rejection sampling, but would be phenomenally ineffective ... Kjetil On Thu, Jul 19, 2012 at 7:12 AM, arunkumar1111 <akpbond007 at gmail.com> wrote:> hi > > My inputs is min=(10,10,10,10,10) and max=(100,100,100,100,100) > total = 300 > i have to generate 5 numbers between min and max and those numbers should > sum upto total > > Can anyone help? > > ----- > Thanks in Advance > Arun > -- > View this message in context: http://r.789695.n4.nabble.com/Generating-Random-Numbers-tp4637020.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
On Thu, Jul 19, 2012 at 04:12:07AM -0700, arunkumar1111 wrote:> hi > > My inputs is min=(10,10,10,10,10) and max=(100,100,100,100,100) > total = 300 > i have to generate 5 numbers between min and max and those numbers should > sum upto totalHi. If we subtract the minimum from each number, then we need to generate 5 numbers from [0, 90], which sum up to 250. The extremal points, which satisfy these conditions, are all permutations of the vector 90, 90, 70, 0, 0 There are 30 of these permutations. Then, it is sufficient to take a random convex combination of these points. Try the following. set.seed(11223344) # generate all permutations of the vector u <- c(90, 90, 70, 0, 0) extremes <- unique(t(replicate(200, u[sample(5)]))) # get a random convex combination x <- diff(c(0, sort(runif(nrow(extremes)-1)), 1)) out <- c(x %*% extremes + 10) out [1] 70.63310 60.11320 67.40902 53.73936 48.10532 sum(out) [1] 300 Hope this helps. Petr Savicky.
On Thu, Jul 19, 2012 at 04:12:07AM -0700, arunkumar1111 wrote:> hi > > My inputs is min=(10,10,10,10,10) and max=(100,100,100,100,100) > total = 300 > i have to generate 5 numbers between min and max and those numbers should > sum upto totalHi. Try the following. while (1) { x <- 10 + 250*diff(c(0, sort(runif(4)), 1)) if (all(x <= 100)) break } x [1] 99.32985 77.04346 65.49482 33.98516 24.14672 sum(x) [1] 300 According to some tests, the average number of repetitions of the while loop is about 5. Hope this helps. Petr Savicky.