[R] Generating Random Numbers

Petr Savicky savicky at cs.cas.cz
Thu Jul 19 22:09:52 CEST 2012


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 total

Hi.

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.



More information about the R-help mailing list