# 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:** 1\
**Showing post:** 3

<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)

---

_[View the full topic](https://discourse.julialang.org/t/a-better-way-to-chose-a-random-neighbor/80979)._
