# A better way to chose a random neighbor

**URL:** <https://discourse.julialang.org/t/a-better-way-to-chose-a-random-neighbor/80979>\
**Category:** New to Julia\
**Tags:** optimization\
**Created:** [May 12, 2022, 8:36pm UTC](https://discourse.julialang.org/t/a-better-way-to-chose-a-random-neighbor/80979 "2022-05-12T20:36:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lsaravia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lsaravia/32/33532_2.png) [@lsaravia](https://discourse.julialang.org/u/lsaravia)\
**Post date:** [May 12, 2022, 8:36pm UTC](https://discourse.julialang.org/t/a-better-way-to-chose-a-random-neighbor/80979/1 "2022-05-12T20:36:13Z")

</div>

I am building a kind of cellular automata and I have a matrix and I would like to choose a random neighbour of a random position, the following is the way I found I wonder if there is a better more efficient way of doing it:

```julia
fil = 10
col = 10
m = zeros(Int16,fil,col)
m[:,1] .= 1

neighborhood = ((0,1),(0,-1),(1,0),(-1,0) )

for z in 1:length(m)
    i= rand(1:fil) 
    j= rand(1:col)
    if m[i,j]==1
        x=[i,j] .+ rand(neighborhood)

        # check boundaries
    
        if !(x[1] > fil || x[1] <1 || x[2] > col || x[2]<1)
           m[x[1],x[2]] = 1 
        end
   
end 

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 12, 2022, 8:41pm UTC](https://discourse.julialang.org/t/a-better-way-to-chose-a-random-neighbor/80979/2 "2022-05-12T20:41:57Z")

</div>

> [@lsaravia](#):
>
> `x=[i,j] .+ rand(neighborhood)`

This allocates an array `[i,j]` on every iteration. Better to use a tuple `(i,j) .+ rand(neighborhood)`.

> [@lsaravia](#):
>
> ```julia
> # check boundaries
> if !(x[1] > fil || x[1] <1 || x[2] > col || x[2]<1)
> 
> ```

I would usually recommend employing [ghost cells](https://discourse.julialang.org/t/finite-difference-laplacian-with-five-point-stencil/25014/2) to avoid these kinds of boundary checks in inner loops.

---

<div class="post-metadata">

**Author:** ![lsaravia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lsaravia/32/33532_2.png) [@lsaravia](https://discourse.julialang.org/u/lsaravia)\
**Post date:** [May 12, 2022, 9:28pm UTC](https://discourse.julialang.org/t/a-better-way-to-chose-a-random-neighbor/80979/3 "2022-05-12T21:28:39Z")

</div>

Thanks! is there an example of how to implement ghost cells?

Anyways that would work for closest neighbours if I have a function that gives a random position with an arbitrary maximum distance e.g.

```julia

 x=(i,j) .+ rand_max_distance(fil)

```

will be more difficult to avoid boundary checks, and something similar would happen with periodic boundary conditions, you would need to use `getindex(a, i) = a.a[i % n]` as you suggested to avoid in [Arrays with periodic boundaries - #4 by stevengj](https://discourse.julialang.org/t/arrays-with-periodic-boundaries/4015/4)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 12, 2022, 11:05pm UTC](https://discourse.julialang.org/t/a-better-way-to-chose-a-random-neighbor/80979/4 "2022-05-12T23:05:25Z")

</div>

> [@lsaravia](#):
>
> Thanks! is there an example of how to implement ghost cells?

The “ghost cells” link [above](https://discourse.julialang.org/t/a-better-way-to-chose-a-random-neighbor/80979/2) has an example. [Here](https://discourse.julialang.org/t/seemingly-unnecessary-allocation-within-for-loop/72556/8) is another example, and [here](https://discourse.julialang.org/t/speed-up-multidimensional-code-without-base-cartesian/68328/14) is another.
