Re: [R] Generate random vectors (continuous number) with a fixed sum

2025-04-22 Thread Rui Barradas
Hello, Here are your tests and the random numbers' histograms. one_vec <- function(a, b, s) { repeat{ repeat{ u <- runif(1, a[1], b[1]) if(s - u > 0) break } v <- s - u if(a[2] < v && v < b[2]) break } c(u, v) } gen_mat <- function(m, a, b, s) { replicate(m,

Re: [R] Generate random vectors (continuous number) with a fixed sum

2025-04-22 Thread Rui Barradas
Hello, Inline. Às 17:55 de 22/04/2025, Brian Smith escreveu: i.e. we should have all elements of Reduce("+", res) should be equal to s = 0.05528650577311 My assertion is that it is not happing here. You are right, that's not what is happening. The output is n vectors of 2 elements each. I

Re: [R] Generate random vectors (continuous number) with a fixed sum

2025-04-22 Thread Brian Smith
i.e. we should have all elements of Reduce("+", res) should be equal to s = 0.05528650577311 My assertion is that it is not happing here. On Tue, 22 Apr 2025 at 22:20, Brian Smith wrote: > > Hi Rui, > > Thanks for the explanation. > > But in this case, are we looking at the correct solution a

Re: [R] Generate random vectors (continuous number) with a fixed sum

2025-04-22 Thread Brian Smith
Hi Rui, Thanks for the explanation. But in this case, are we looking at the correct solution at all? My goal is to generate random vector where: 1) the first element is bounded by (a[1], b[1]) and second element is bounded by (a[2], b[2]) 2) sum of the element is s According to the outcome, The

Re: [R] Generate random vectors (continuous number) with a fixed sum

2025-04-22 Thread Rui Barradas
Às 12:39 de 22/04/2025, Brian Smith escreveu: Hi Rui, Many thanks for your time and insight. However, I am not sure if I could understand the code. Below is what I tried based on your code library(Surrogate) a <- c(0.015, 0.005) b <- c(0.070, 0.045) set.seed(2025) res <- mapply(\(a, b, s, n, m

Re: [R] Generate random vectors (continuous number) with a fixed sum

2025-04-22 Thread Brian Smith
Hi Rui, Many thanks for your time and insight. However, I am not sure if I could understand the code. Below is what I tried based on your code library(Surrogate) a <- c(0.015, 0.005) b <- c(0.070, 0.045) set.seed(2025) res <- mapply(\(a, b, s, n, m) RandVec(a, b, s, n, m), MoreArg