Rui Barradas
2025-Apr-21 15:08 UTC
[R] Generate random vectors (continuous number) with a fixed sum
?s 15:27 de 21/04/2025, Brian Smith escreveu:> Hi, > > There is a function called RandVec in the package Surrogate which can > generate andom vectors (continuous number) with a fixed sum > > The help page of this function states that: > > a > > The function RandVec generates an n by m matrix x. Each of the m > columns contain n random values lying in the interval [a,b]. The > argument a specifies the lower limit of the interval. Default 0. > > b > > The argument b specifies the upper limit of the interval. Default 1. > > However in my case, the lower and upper limits are not same. For > example, if I need to draw a pair of number x, y, such that x + y = 1, > then the lower and upper limits are different. > > I tried with below code > > library(Surrogate) > > RandVec(a=c(0.1, 0.2), b=c(0.2, 0.8), s=1, n=2, m=5)$RandVecOutput > > This generates error with message > > Error in if (b - a == 0) { : the condition has length > 1 > > Is there any way to generate the numbers with different lower and upper limits? > > ______________________________________________ > 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 https://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.Hello, Use ?mapply to cycle through all values of a and b. Note that the output matrices are transposed, the random vectors are the rows. library(Surrogate) a <- c(0.1, 0.2) b <- c(0.2, 0.8) mapply(\(a, b, s, n, m) RandVec(a, b, s, n, m), MoreArgs = list(s = 1, n = 2, m = 5), a, b) #> $RandVecOutput #> [,1] [,2] [,3] [,4] [,5] #> [1,] 0.2004363 0.1552328 0.2391742 0.1744857 0.1949236 #> [2,] 0.1995637 0.2447672 0.1608258 0.2255143 0.2050764 #> #> $RandVecOutput #> [,1] [,2] [,3] [,4] [,5] #> [1,] 0.2157416 0.4691191 0.5067447 0.7749258 0.7728955 #> [2,] 0.7842584 0.5308809 0.4932553 0.2250742 0.2271045 Hope this helps, Rui Barradas -- Este e-mail foi analisado pelo software antiv?rus AVG para verificar a presen?a de v?rus. www.avg.com
Rui Barradas
2025-Apr-21 15:13 UTC
[R] Generate random vectors (continuous number) with a fixed sum
Hello, Inline. ?s 16:08 de 21/04/2025, Rui Barradas escreveu:> ?s 15:27 de 21/04/2025, Brian Smith escreveu: >> Hi, >> >> There is a function called RandVec in the package Surrogate which can >> generate andom vectors (continuous number) with a fixed sum >> >> The help page of this function states that: >> >> a >> >> The function RandVec generates an n by m matrix x. Each of the m >> columns contain n random values lying in the interval [a,b]. The >> argument a specifies the lower limit of the interval. Default 0. >> >> b >> >> The argument b specifies the upper limit of the interval. Default 1. >> >> However in my case, the lower and upper limits are not same. For >> example, if I need to draw a pair of number x, y, such that x + y = 1, >> then the lower and upper limits are different. >> >> I tried with below code >> >> library(Surrogate) >> >> RandVec(a=c(0.1, 0.2), b=c(0.2, 0.8), s=1, n=2, m=5)$RandVecOutput >> >> This generates error with message >> >> Error in if (b - a == 0) { : the condition has length > 1 >> >> Is there any way to generate the numbers with different lower and >> upper limits? >> >> ______________________________________________ >> 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 https://www.R-project.org/posting- >> guide.html >> and provide commented, minimal, self-contained, reproducible code. > Hello, > > Use ?mapply to cycle through all values of a and b. > Note that the output matrices are transposed, the random vectors are the > rows.Sorry, this is not true. The columns are the random vectors, as documented. An example setting the RNG seed, for reproducibility. library(Surrogate) a <- c(0.1, 0.2) b <- c(0.2, 0.8) set.seed(2025) res <- mapply(\(a, b, s, n, m) RandVec(a, b, s, n, m), MoreArgs = list(s = 1, n = 2, m = 5), a, b) res #> $RandVecOutput #> [,1] [,2] [,3] [,4] [,5] #> [1,] 0.146079 0.1649319 0.1413759 0.257086 0.1715478 #> [2,] 0.253921 0.2350681 0.2586241 0.142914 0.2284522 #> #> $RandVecOutput #> [,1] [,2] [,3] [,4] [,5] #> [1,] 0.5930918 0.2154583 0.6915523 0.7167089 0.3617918 #> [2,] 0.4069082 0.7845417 0.3084477 0.2832911 0.6382082 lapply(res, colSums) #> $RandVecOutput #> [1] 0.4 0.4 0.4 0.4 0.4 #> #> $RandVecOutput #> [1] 1 1 1 1 1 Hope this helps, Rui Barradas> > > library(Surrogate) > > a <- c(0.1, 0.2) > b <- c(0.2, 0.8) > mapply(\(a, b, s, n, m) RandVec(a, b, s, n, m), > ?????? MoreArgs = list(s = 1, n = 2, m = 5), a, b) > #> $RandVecOutput > #>?????????? [,1]????? [,2]????? [,3]????? [,4]????? [,5] > #> [1,] 0.2004363 0.1552328 0.2391742 0.1744857 0.1949236 > #> [2,] 0.1995637 0.2447672 0.1608258 0.2255143 0.2050764 > #> > #> $RandVecOutput > #>?????????? [,1]????? [,2]????? [,3]????? [,4]????? [,5] > #> [1,] 0.2157416 0.4691191 0.5067447 0.7749258 0.7728955 > #> [2,] 0.7842584 0.5308809 0.4932553 0.2250742 0.2271045 > > > Hope this helps, > > Rui Barradas > >-- Este e-mail foi analisado pelo software antiv?rus AVG para verificar a presen?a de v?rus. www.avg.com