0

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.

LDT
  • 2,856
  • 2
  • 15
  • 32
  • I don't understand. Can you elaborate some more? – user2974951 Mar 16 '20 at 08:56
  • Yes, of course :). I want from a range of values to extract arrays of five numbers that all have the same distance to each other. I would like the desired distance to be 0.02. If you run numbers <- seq(0.50, 0.75, length=100) then you get 100 different numbers between 0.50 and 0.75. From that range, I want to extract arrays of 5 numbers with 0.02 distance. Does this make more sense? – LDT Mar 16 '20 at 09:00
  • So you want to generate n random numbers between 0.5 and 0.75 and then generate an array with 5 elements starting from the selected random number x to x+0.1 by 0.2? – user2974951 Mar 16 '20 at 09:04
  • Yes you got it right – LDT Mar 16 '20 at 10:30

2 Answers2

1

It's not clear if this is exactly what you're looking for. Do the starting values all need to be multiples of 0.02 as well? If so, these are the 9 possible sets that match your brief.

lapply(seq(0.5, 0.66, 0.02), function(x) x + 0.02*0:4)
#> [[1]]
#> [1] 0.50 0.52 0.54 0.56 0.58
#> 
#> [[2]]
#> [1] 0.52 0.54 0.56 0.58 0.60
#> 
#> [[3]]
#> [1] 0.54 0.56 0.58 0.60 0.62
#> 
#> [[4]]
#> [1] 0.56 0.58 0.60 0.62 0.64
#> 
#> [[5]]
#> [1] 0.58 0.60 0.62 0.64 0.66
#> 
#> [[6]]
#> [1] 0.60 0.62 0.64 0.66 0.68
#> 
#> [[7]]
#> [1] 0.62 0.64 0.66 0.68 0.70
#> 
#> [[8]]
#> [1] 0.64 0.66 0.68 0.70 0.72
#> 
#> [[9]]
#> [1] 0.66 0.68 0.70 0.72 0.74

If the starting numbers can take any value, then a better solution would be

runif(1, 0.5, 0.67) + 0.02 * 0:4

Which you can put into a loop or lapply to get any number of samples like this:

lapply(seq(5), function(x) runif(1, 0.5, 0.67) + 0.02 * 0:4)
#> [[1]]
#> [1] 0.5973477 0.6173477 0.6373477 0.6573477 0.6773477
#> 
#> [[2]]
#> [1] 0.5174972 0.5374972 0.5574972 0.5774972 0.5974972
#> 
#> [[3]]
#> [1] 0.6529702 0.6729702 0.6929702 0.7129702 0.7329702
#> 
#> [[4]]
#> [1] 0.5418349 0.5618349 0.5818349 0.6018349 0.6218349
#> 
#> [[5]]
#> [1] 0.5071501 0.5271501 0.5471501 0.5671501 0.5871501

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
1
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.

user2974951
  • 9,535
  • 1
  • 17
  • 24