# Need help understanding the shuffle cards method

**URL:** <https://discourse.processing.org/t/need-help-understanding-the-shuffle-cards-method/1247>\
**Category:** Coding Questions\
**Created:** [June 25, 2018, 2:58pm UTC](https://discourse.processing.org/t/need-help-understanding-the-shuffle-cards-method/1247 "2018-06-25T14:58:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ProcessingPro](https://avatars.discourse-cdn.com/v4/letter/p/f1d935/32.png) [@ProcessingPro](https://discourse.processing.org/u/ProcessingPro)\
**Post date:** [June 25, 2018, 2:58pm UTC](https://discourse.processing.org/t/need-help-understanding-the-shuffle-cards-method/1247/1 "2018-06-25T14:58:54Z")

</div>

I don’t understand the //shuffle cards thing.

```auto
 //assigning values to each card in the grid
  int[] values = new int[20];
  for (int i=0; i<icons.length; i++) {
   icons[i] = loadImage(filename[i]);
    values[2*i] = i;
    values[2*i+1] = i;
  }

  //shuffle cards
  for (int i=0; i<values.length; i++) {
    int r = int(random(values.length));
    int t = values[i];
    values[i] = values[r];
    values[r] = t;
  }

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 25, 2018, 3:23pm UTC](https://discourse.processing.org/t/need-help-understanding-the-shuffle-cards-method/1247/2 "2018-06-25T15:23:59Z")

</div>

[https://Bost.ocks.org/mike/shuffle/](https://Bost.ocks.org/mike/shuffle/)

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [June 25, 2018, 4:32pm UTC](https://discourse.processing.org/t/need-help-understanding-the-shuffle-cards-method/1247/3 "2018-06-25T16:32:47Z")

</div>

> [@ProcessingPro](#):
>
> int t = values[i];  
> values[i] = values[r];  
> values[r] = t;

This part does a swapping of two items in your array. Your i counter is going through every element in your number container in order, from the first to the last. The r counter is a random item in your list, calculated every time you step in your array.

Kf

---

<div class="post-metadata">

**Author:** ![ProcessingPro](https://avatars.discourse-cdn.com/v4/letter/p/f1d935/32.png) [@ProcessingPro](https://discourse.processing.org/u/ProcessingPro)\
**Post date:** [June 25, 2018, 6:13pm UTC](https://discourse.processing.org/t/need-help-understanding-the-shuffle-cards-method/1247/4 "2018-06-25T18:13:16Z")

</div>

thank you 🙂
