Search code examples
rrstudioseqlinspace

Create sequences of random numbers with the same distance in R


I am trying to create sequences of five random numbers, within a range of values, where the elements have the same distance to each other. I am an R user.

in more details: within that range of numbers numbers <- seq(0.50,0.75,length=100)

I want to select as many as possible random sequences where each element has 0.02 difference to each other (0.50,0.52, 0.54, 0.56, 0.58) (0.52, 0.54, 0.56, 0.58, 0.60) ...... (0.60, 0.62, 0.64, 0.66, 0.68)

Any help would be highly appreciated.


Solution

  • set.seed(0)
    replicate(5,{
      x=runif(1,0.5,0.75)
      seq(x,x+0.0.8,0.02)
    })
              [,1]      [,2]      [,3]      [,4]      [,5]
    [1,] 0.5504205 0.7245974 0.7361688 0.6651994 0.6572785
    [2,] 0.5704205 0.7445974 0.7561688 0.6851994 0.6772785
    [3,] 0.5904205 0.7645974 0.7761688 0.7051994 0.6972785
    [4,] 0.6104205 0.7845974 0.7961688 0.7251994 0.7172785
    [5,] 0.6304205 0.8045974 0.8161688 0.7451994 0.7372785
    

    The 5 different arrays are in the columns.