Consider a vector of numbers. I would like to sample a number which is between a given position in the vector and its neighbors such that the two closest neighbors have the largest impact. Also, this impact is in the decreasing according to the distance from the reference point.

For example, consider the following vector:

vec = c(15, 16, 18, 21, 24, 30, 31)

and my reference is the number 16 in position #2. I would like to sample a number which will be with a high probability between 15 and 16 or (with the same high probability) between 16 and 18. The sampled numbers can be floats. Then, with a decreasing probability to sample a number between 16 and 21, and with a yet lower probability between 16 and 24, and so on.

The position of the reference is not known in advance, it can be anywhere in the vector.

I tried using with runif and quantiles, but I'm not sure how to design the scores of the neighbors.

Specifically, I wrote the following function but I suspect there might be a better/more efficient way of doing this:

GenerateNumbers <- function(Ind,N){
dist <- 1/abs(Ind- 1:length(N))
dist <- dist[!is.infinite(dist)]
dist <- dist/sum(dist)
sum(dist) #sanity check --> 1
V = numeric(length(N) - 1)
for (i in 1:(length(N)-1)) {
V[i] = runif(1, N[i], N[i+1])
}
sample(V,1,prob = dist)
}

where Ind is the position of the reference number (16 in this case), and N is the vector. "Dist" is a way of weighing the probabilities so that the closest neighbors have a higher impact.

Any help is highly appreciated.