# Shuffle a part of an array

**URL:** <https://discourse.julialang.org/t/shuffle-a-part-of-an-array/87982>\
**Category:** General Usage\
**Tags:** question, arrays\
**Created:** [September 29, 2022, 11:34am UTC](https://discourse.julialang.org/t/shuffle-a-part-of-an-array/87982 "2022-09-29T11:34:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nethaji8](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@nethaji8](https://discourse.julialang.org/u/nethaji8)\
**Post date:** [September 29, 2022, 11:34am UTC](https://discourse.julialang.org/t/shuffle-a-part-of-an-array/87982/1 "2022-09-29T11:34:34Z")

</div>

Hi everyone,  
I am new to julia, can someone please let me know how to shuffle a part of an array in julia using the shuffle function…?

```julia
using Random

A=collect(1:10)

shuffle!(A[1:9])
println(A[1:9])

shuffle!(A)
println(A)

```

output

```julia
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 10, 6, 8, 9, 3, 5, 7, 4, 1]

```

You can observe that `shuffle!(A[1:9])` is not working.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 29, 2022, 11:38am UTC](https://discourse.julialang.org/t/shuffle-a-part-of-an-array/87982/2 "2022-09-29T11:38:08Z")

</div>

```julia
julia> A=collect(1:10);

julia> shuffle!(view(A,1:9));

julia> A
10-element Vector{Int64}:
  8
  2
  6
  4
  5
  1
  7
  3
  9
 10

```

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [September 29, 2022, 12:07pm UTC](https://discourse.julialang.org/t/shuffle-a-part-of-an-array/87982/3 "2022-09-29T12:07:39Z")

</div>

**Explanation** :  
When you do

> [@nethaji8](#):
>
> `shuffle!(A[1:9])`

`A[1:9]` is separated out as a _new_ (temporary) array, with its own separate memory, so `shuffle!` only gets to modify that. `A` itself remains unchanged.

Doing `view(A, 1:9)` or `@view(A[1:9])` (both do the same thing) gives you access to that part of the array without allocating in a different new memory spot. This lets `shuffle!` modify the contents of `A` itself.

`view` is a very useful feature to know for performance too, because allocating new memory can be costly, and `view`s avoid that. So when you need high performance in some part of the code and are accessing slices of arrays like `A[1:9]` in it, changing those into `@view`s can give a good performance boost (unless you actually need a _copy_ of the values, of course).

---

<div class="post-metadata">

**Author:** ![nethaji8](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@nethaji8](https://discourse.julialang.org/u/nethaji8)\
**Post date:** [September 30, 2022, 9:46am UTC](https://discourse.julialang.org/t/shuffle-a-part-of-an-array/87982/4 "2022-09-30T09:46:54Z")

</div>

Thank you for your help @digital_carver

---

<div class="post-metadata">

**Author:** ![nethaji8](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@nethaji8](https://discourse.julialang.org/u/nethaji8)\
**Post date:** [September 30, 2022, 9:47am UTC](https://discourse.julialang.org/t/shuffle-a-part-of-an-array/87982/5 "2022-09-30T09:47:27Z")

</div>

Thank you for your help @oheil
