# Why is \`sort(x, by = \_ -\> rand())\` not a good shuffler?

**URL:** <https://discourse.julialang.org/t/why-is-sort-x-by-rand-not-a-good-shuffler/76737>\
**Category:** General Usage\
**Created:** [February 19, 2022, 11:56am UTC](https://discourse.julialang.org/t/why-is-sort-x-by-rand-not-a-good-shuffler/76737 "2022-02-19T11:56:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [February 19, 2022, 11:56am UTC](https://discourse.julialang.org/t/why-is-sort-x-by-rand-not-a-good-shuffler/76737/1 "2022-02-19T11:56:01Z")

</div>

I learned this the hard way in that `sort(x, by = _ -> rand())` is not a shuffler, i.e. `x` is not random enough after this shuffle.

Using `Random.shuffle` is the correct way it seems.

But why is `sort(x, by = _ -> rand())` not that great at shuffling?

is it cos `rand()` runs too fast so the same random number get for some successive numbers? This can’t be the case since doesn’t every run of `rand()` generate a different number if the random seed is not reset.

```julia
res = mapreduce(vcat, 1:1000) do _

    reshape(sort(1:16, by = _ -> rand()), 1, :)

end

mean(res[:, 1]) # 2.5

res2 = mapreduce(vcat, 1:1000) do _

    reshape(shuffle(1:16), 1, :)

end

mean(res2[:, 1]) # 8.342

```

To see the effect, consider the above where I shuffled the numbers `1:16` using the 2 methods and calculated the mean of the first number. Clearning the first method is too low, meaning not enough small number get shuffled to the end.

---

<div class="post-metadata">

**Author:** ![Dictino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dictino/32/217281_2.png) [@Dictino](https://discourse.julialang.org/u/Dictino)\
**Post date:** [February 19, 2022, 12:13pm UTC](https://discourse.julialang.org/t/why-is-sort-x-by-rand-not-a-good-shuffler/76737/2 "2022-02-19T12:13:54Z")

</div>

I guess that the problem is precisely that the result of rand is changing on each run.

If you create a random column and sort by this colum It must be fine.

But as rand gives you something new on each comparison if the Sort algorithm, the element is doing a “random walk” sometimes it goes to the front and sometimes ir goes back and on average It ends more ore less where It starts

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [February 19, 2022, 12:15pm UTC](https://discourse.julialang.org/t/why-is-sort-x-by-rand-not-a-good-shuffler/76737/3 "2022-02-19T12:15:11Z")

</div>

That makes sense, since the `rand()` is run every time and not fixed. I had thought for some reason taht the random number only get generated once.
