u <- runif (410) u <- (u - min (u) ) / diff (range (u) ) constrained.sample <- function (rate) { plim <- pexp (c (9.6, 11.6), rate) p <- plim [1] + diff (plim) * u qexp (p, rate) } diff.sum <- function (rate) sum (constrained.sample (rate) ) - 4200 rate <- uniroot (diff.sum, c (1, 2) )$root q <- constrained.sample (rate) length (q) range (q) sum (q) On Wed, Jan 27, 2021 at 9:03 PM Denis Francisci <denis.francisci at gmail.com> wrote:> > Hi, > I would like to generate random numbers in R with some constraints: > - my vector of numbers must contain 410 values; > - min value must be 9.6 and max value must be 11.6; > - sum of vector's values must be 4200. > Is there a way to do this in R? > And is it possible to generate this series in such a way that it follows a > specific distribution form (for example exponential)? > Thank you in advance, > > D. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Wonderful! This is exactly what I need! Thank you very much!! Denis Il giorno mer 27 gen 2021 alle ore 10:58 Abby Spurdle <spurdle.a at gmail.com> ha scritto:> u <- runif (410) > u <- (u - min (u) ) / diff (range (u) ) > > constrained.sample <- function (rate) > { plim <- pexp (c (9.6, 11.6), rate) > p <- plim [1] + diff (plim) * u > qexp (p, rate) > } > > diff.sum <- function (rate) > sum (constrained.sample (rate) ) - 4200 > > rate <- uniroot (diff.sum, c (1, 2) )$root > q <- constrained.sample (rate) > > length (q) > range (q) > sum (q) > > > On Wed, Jan 27, 2021 at 9:03 PM Denis Francisci > <denis.francisci at gmail.com> wrote: > > > > Hi, > > I would like to generate random numbers in R with some constraints: > > - my vector of numbers must contain 410 values; > > - min value must be 9.6 and max value must be 11.6; > > - sum of vector's values must be 4200. > > Is there a way to do this in R? > > And is it possible to generate this series in such a way that it follows > a > > specific distribution form (for example exponential)? > > Thank you in advance, > > > > D. > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > 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. >[[alternative HTML version deleted]]