Re: [R] random numbers with constraints

2021-01-28 Thread Abby Spurdle
I recognize the problems with global data. And my code could certainly be improved. However, I also note that the random numbers (ignoring transformations), need to be constant, while computing the rate. Otherwise, my algorithm wouldn't work well. As it is, rounding operations can cause "jumps".

Re: [R] random numbers with constraints

2021-01-28 Thread Martin Maechler
> Abby Spurdle > on Thu, 28 Jan 2021 08:48:06 +1300 writes: > I note that there's a possibility of floating point errors. > If all values have one digit after the decimal point, you could replace: > qexp (p, rate) with round (qexp (p, rate), 1). > However, sometimes u

Re: [R] random numbers with constraints

2021-01-28 Thread Denis Francisci
Thanks again for your help, One digit after the decimal point is enough for my purposes; so, I can round the qexp function, even if possible errors in floating points are not a problem. Thank you very very much, Denis Il giorno mer 27 gen 2021 alle ore 20:48 Abby Spurdle ha scritto: > I note

Re: [R] random numbers with constraints

2021-01-27 Thread Abby Spurdle
I note that there's a possibility of floating point errors. If all values have one digit after the decimal point, you could replace: qexp (p, rate) with round (qexp (p, rate), 1). However, sometimes uniroot will fail, due to problems with input. On Thu, Jan 28, 2021 at 5:02 AM Denis Francisci wr

Re: [R] random numbers with constraints

2021-01-27 Thread Denis Francisci
Wonderful! This is exactly what I need! Thank you very much!! Denis Il giorno mer 27 gen 2021 alle ore 10:58 Abby Spurdle 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

Re: [R] random numbers with constraints

2021-01-27 Thread Abby Spurdle
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,

Re: [R] random numbers with constraints

2021-01-27 Thread Ralf Goertz
Am Wed, 27 Jan 2021 09:03:15 +0100 schrieb Denis Francisci : > 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

[R] random numbers with constraints

2021-01-27 Thread Denis Francisci
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