# Shuffle an array

**URL:** <https://forum.dfinity.org/t/shuffle-an-array/2077>\
**Category:** Language Support\
**Created:** [March 3, 2021, 11:15am UTC](https://forum.dfinity.org/t/shuffle-an-array/2077 "2021-03-03T11:15:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![chchrist](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/chchrist/32/764_2.png) [@chchrist](https://forum.dfinity.org/u/chchrist)\
**Post date:** [March 3, 2021, 11:15am UTC](https://forum.dfinity.org/t/shuffle-an-array/2077/1 "2021-03-03T11:15:25Z")

</div>

I’m trying to shuffle an array and I’m not sure what is the right way with Motoko.

This is what I have so far:

```
func shuffle(cards:[Card]) : [Card] {
    var currentIndex = cards.size();

    while(0 != currentIndex) {
        var randomIndex = Float.toInt(Float.floor(Math.random() * Float.fromInt(currentIndex)));
        currentIndex -= 1;
        var tempValue = cards[currentIndex];
        cards[currentIndex] := cards[randomIndex];
        cards[randomIndex] := tempValue;
        D.print(debug_show(randomIndex));
        D.print(debug_show(currentIndex));
        D.print(debug_show(tempValue.id));
};

return cards;

```

};

and I’m getting these errors:

```
Stderr:
main.mo:24.9-24.50: type error [M0073], expected mutable assignment target
main.mo:25.15-25.26: type error [M0096], expression of type
  Int
cannot produce expected type
  Nat
main.mo:25.9-25.40: type error [M0073], expected mutable assignment target

```

It seems I don’t know how to replace assign new values to array items and also it seems that the index of an array needs to be a Nat but I have no clue how to convert an Int to a Nat…  
Math.random() generates pseudo random Floats between 0 and 1.

---

<div class="post-metadata">

**Author:** ![chchrist](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/chchrist/32/764_2.png) [@chchrist](https://forum.dfinity.org/u/chchrist)\
**Post date:** [March 3, 2021, 12:04pm UTC](https://forum.dfinity.org/t/shuffle-an-array/2077/2 "2021-03-03T12:04:36Z")

</div>

I wonder if the problem is that the array I’m passing to the function is immutable?  
Should I use Array.tabulate to generate a mutable array from the immutable one?  
Is there an easier way to copy an array?

---

<div class="post-metadata">

**Author:** ![Hazel](https://avatars.discourse-cdn.com/v4/letter/h/e56c9b/32.png) [@Hazel](https://forum.dfinity.org/u/Hazel)\
**Post date:** [March 3, 2021, 3:16pm UTC](https://forum.dfinity.org/t/shuffle-an-array/2077/3 "2021-03-03T15:16:00Z")

</div>

This is going to sound silly, two things:

First,

```auto
let foo : [Card] = []; // Elements are immutable
let bar : [var Card] = []; Elements are mutable! 

```

Second, see below post! (edited)

---

<div class="post-metadata">

**Author:** ![kritzcreek](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/kritzcreek/32/267_2.png) [@kritzcreek](https://forum.dfinity.org/u/kritzcreek)\
**Post date:** [March 3, 2021, 4:29pm UTC](https://forum.dfinity.org/t/shuffle-an-array/2077/4 "2021-03-03T16:29:33Z")

</div>

[Int :: Internet Computer](https://sdk.dfinity.org/docs/base-libraries/int#value.abs) is probably the easiest way to get a `Nat` from an `Int`, if you’re 100% sure you’ll never look at a negative one. Otherwise you’ll have to guard with a `someInt >= 0` check to make sure you’re not breaking invariants.

---

<div class="post-metadata">

**Author:** ![Hazel](https://avatars.discourse-cdn.com/v4/letter/h/e56c9b/32.png) [@Hazel](https://forum.dfinity.org/u/Hazel)\
**Post date:** [March 3, 2021, 5:01pm UTC](https://forum.dfinity.org/t/shuffle-an-array/2077/5 "2021-03-03T17:01:23Z")

</div>

@kritzcreek - thank you for pointing that out! I can’t believe I’ve managed to miss that this entire time 🤦‍♂️ .

---

<div class="post-metadata">

**Author:** ![chchrist](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/chchrist/32/764_2.png) [@chchrist](https://forum.dfinity.org/u/chchrist)\
**Post date:** [March 3, 2021, 5:27pm UTC](https://forum.dfinity.org/t/shuffle-an-array/2077/6 "2021-03-03T17:27:12Z")

</div>

ty @kritzcreek that worked!

@Hazel

`let bar : [var Card] = [];`  
and  
`var bar: [var Card] = []`

returns

`type error [M0091], immutable array expression cannot produce expected type [var Card/1]`

---

<div class="post-metadata">

**Author:** ![nomeata](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/nomeata/32/733_2.png) [@nomeata](https://forum.dfinity.org/u/nomeata)\
**Post date:** [March 3, 2021, 6:54pm UTC](https://forum.dfinity.org/t/shuffle-an-array/2077/7 "2021-03-03T18:54:04Z")

</div>

The empty array with mutable elements is `[var]`, not `[]`.

Typically, if you use

```auto
var bar : […]

```

i.e. use a mutable variable for the whole array, one doesn’t generally also need the elements to be mutable.
