Re: [R] Simulating mid-points from a defined range

2025-06-04 Thread Richard O'Keefe
The Bentley and Saxe paper answers the questions (1) How can I draw a sample of n numbers in the range 0..1 such that the numbers are returned in increasing order WITHOUT sorting and so that all such sequences are equally likely in LINEAR time. That's the code I showed in R. (2) How can I do this

Re: [R] Simulating mid-points from a defined range

2025-06-04 Thread Bert Gunter
Is Peter's solution different then: diffs <- cumsum(runif(9, 5, 100/9)) x <-runif(1,0,100-diffs[9]) c(x, x+diffs) I ask because: 1. If yes, this is why more context is needed; 2. If no, the above avoids a sort. Cheers, Bert On Tue, Jun 3, 2025 at 2:15 PM peter dalgaard wrote: > Can't you j

Re: [R] Simulating mid-points from a defined range

2025-06-04 Thread Brian Smith
Hi, I dont see from the solution pov they are different. One followup Q though, how can I extend this to draw only integer mid-point between 0-100 while maintaining minimum difference as 5? Also, are all generated points are equally likely? Thanks for your time and suggestions. Thanks and rega

Re: [R] Simulating mid-points from a defined range

2025-06-04 Thread Brian Smith
Hi Peter, Could you please help me to understand what is the basis of choosing 55 in runif(10,0,55))? Thank you! On Wed, 4 Jun 2025 at 02:45, peter dalgaard wrote: > > Can't you just generate 10 values in (0,55), sort them, generate the > distances, add 5 and cumulate? > > > x <- sort(runif(10

Re: [R] Simulating mid-points from a defined range

2025-06-04 Thread Brian Smith
Okay, I think I found the reason. This is due to accumulation of nine 5s in the cumsum. Thanks again for the elegant solution. But I wonder, if the solution is simple then what is the significance of the Research paper by Bentley and Saxe naming “Generating sorted lists of random numbers” which Ri

Re: [R] Simulating mid-points from a defined range

2025-06-04 Thread Sergei Ko
Hi! It's possible I'm wrong. But for me it looks like: n_sample <- 300 v <- round(runif(n_sample, 0, 100), 2) m <- matrix(v, ncol = 10) m_s <- t(apply(m, 1, sort)) m_d <- t(apply(m_s, 1, diff)) m_5 <- m_d>=5 f <- rowSums(m_5) == 9 sum(f) res <- m_s[f, ] Regards, Sergiy On Wed, Jun 4, 2025 at 1

Re: [R] Simulating mid-points from a defined range

2025-06-04 Thread Bert Gunter
To answer my own question, yes they are different. Peter's code can generate the solution 1 20 26 32 38 44 50 56 62 68. Mine cannot. So, again, context? -- Bert On Wed, Jun 4, 2025 at 4:43 AM Bert Gunter wrote: > Is Peter's solution different then: > > diffs <- cumsum(runif(9, 5, 100/9)) > x

Re: [R] Simulating mid-points from a defined range

2025-06-04 Thread Bert Gunter
In Peter's solution, just sample without replacement from integers instead of generating random uniforms. See ?sample. -- Bert On Wed, Jun 4, 2025 at 5:23 AM Brian Smith wrote: > Hi, > > I dont see from the solution pov they are different. > > One followup Q though, how can I extend this to dr