# Best way to shuffle elements in a vector of vectors

**URL:** <https://discourse.julialang.org/t/best-way-to-shuffle-elements-in-a-vector-of-vectors/17357>\
**Category:** General Usage\
**Created:** [November 9, 2018, 5:19pm UTC](https://discourse.julialang.org/t/best-way-to-shuffle-elements-in-a-vector-of-vectors/17357 "2018-11-09T17:19:28Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [November 9, 2018, 6:05pm UTC](https://discourse.julialang.org/t/best-way-to-shuffle-elements-in-a-vector-of-vectors/17357/4 "2018-11-09T18:05:40Z")

</div>

If vector-of-vectors is the structure you want then you can do this:

```julia
julia> using Random

julia> data = [collect(1:5) for i in 1:10];

julia> foreach(shuffle!, data)

julia> data
10-element Array{Array{Int64,1},1}:
 [3, 2, 5, 1, 4]
 [1, 2, 4, 5, 3]
 [2, 3, 1, 5, 4]
 [2, 4, 5, 3, 1]
 [2, 3, 4, 5, 1]
 [3, 1, 4, 5, 2]
 [1, 4, 5, 3, 2]
 [5, 3, 1, 4, 2]
 [4, 5, 3, 2, 1]
 [3, 2, 4, 5, 1]

```

If you’d prefer to have a matrix with shuffled rows or columns, you can use views:

```julia
julia> data = [j for i = 1:10, j = 1:5];

julia> for i = 1:size(data, 1)
           shuffle!(@view data[i,:])
       end

julia> data
10×5 Array{Int64,2}:
 2 5 3 4 1
 4 2 1 5 3
 4 5 3 2 1
 1 4 2 3 5
 5 2 1 3 4
 1 4 3 2 5
 4 3 5 2 1
 2 4 3 5 1
 3 1 2 5 4
 3 1 4 2 5

```

---

_[View the full topic](https://discourse.julialang.org/t/best-way-to-shuffle-elements-in-a-vector-of-vectors/17357)._
