# How to make a random selection keeping minimum distance between values

**URL:** <https://discourse.julialang.org/t/how-to-make-a-random-selection-keeping-minimum-distance-between-values/115315>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 7, 2024, 8:58am UTC](https://discourse.julialang.org/t/how-to-make-a-random-selection-keeping-minimum-distance-between-values/115315 "2024-06-07T08:58:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![VinodV](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinodv/32/32733_2.png) [@VinodV](https://discourse.julialang.org/u/VinodV)\
**Post date:** [June 7, 2024, 8:58am UTC](https://discourse.julialang.org/t/how-to-make-a-random-selection-keeping-minimum-distance-between-values/115315/1 "2024-06-07T08:58:56Z")

</div>

```julia
m = 100
n = 10
b = rand(1:m,n)
b = sort(b)

```

I want the minimum distance between adjacent values of `b` to be \>= 2.  
How can I do that.  
I have tried

```julia
b = rand(1:m,n)                    
											
	while true 
		if length(Set(b)) == length(b)			
			
			b = sort(b)
			c = diff(b)
			if true == all(i -> i >= 2,c)
				break
			end
		else
			b = rand(1:m,n)
		end
	end

```

But this is taking a long time (almost infinite loop)

---

<div class="post-metadata">

**Author:** ![VinodV](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinodv/32/32733_2.png) [@VinodV](https://discourse.julialang.org/u/VinodV)\
**Post date:** [June 7, 2024, 9:20am UTC](https://discourse.julialang.org/t/how-to-make-a-random-selection-keeping-minimum-distance-between-values/115315/2 "2024-06-07T09:20:28Z")

</div>

I have found out an answer by adapting python code to julia :

> a = [2\*i + x for (i, x) in enumerate(sort!(rand(1:m, n)))]

but these values are going above m.

> <https://stackoverflow.com/questions/51918580/python-random-list-of-numbers-in-a-range-keeping-with-a-minimum-distance>

---

<div class="post-metadata">

**Author:** ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)\
**Post date:** [June 7, 2024, 12:05pm UTC](https://discourse.julialang.org/t/how-to-make-a-random-selection-keeping-minimum-distance-between-values/115315/3 "2024-06-07T12:05:45Z")

</div>

Fundamentally there may not be `n` numbers in the range `1:m` with minimum difference 2. And when m \>\> n it’s not clear precisely what distribution you want the numbers to come from.

That being said, a slightly better approach would be to generate the differences, perhaps from a geometric distribution:

```julia
using Distributions
p = 0.5
while true
  b = cumsum(rand(Geometric(p), n) .+ 2)
  last(b) <= m && break
end

```

Adjusting `p` will give different distributions, and large `p` and/or 2n close to m will result in the loop running longer.

---

<div class="post-metadata">

**Author:** ![kimikage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kimikage/32/14534_2.png) [@kimikage](https://discourse.julialang.org/u/kimikage)\
**Post date:** [June 7, 2024, 12:12pm UTC](https://discourse.julialang.org/t/how-to-make-a-random-selection-keeping-minimum-distance-between-values/115315/4 "2024-06-07T12:12:34Z")

</div>

As screw\_dog pointed out, this depends on what kind of randomness you want.  
But if you simply want to keep the maximum value below `m`, why not just use `(1-k):(m-k*n)` instead of `1:m`?

```julia
k = 2
[k*i + x for (i, x) in enumerate(sort!(rand((1-k):(m-k*n), n)))]

```
