# Chapter 19, Math.random I'm confused

**URL:** <https://forum.kirupa.com/t/chapter-19-math-random-im-confused/652661>\
**Category:** Uncategorized\
**Created:** [July 16, 2022, 11:44am UTC](https://forum.kirupa.com/t/chapter-19-math-random-im-confused/652661 "2022-07-16T11:44:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevin321](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kevin321/32/18265_2.png) [@kevin321](https://forum.kirupa.com/u/kevin321)\
**Post date:** [July 16, 2022, 11:44am UTC](https://forum.kirupa.com/t/chapter-19-math-random-im-confused/652661/1 "2022-07-16T11:44:11Z")

</div>

Hello, I’m half way through the javascript book, it’s great so far but I am stuck at chapter 19 and wondered if anyone can help me.

When I look at the following code, I can see how Math.random can possibly generate a duplicate random number during the loop. When I run the code it actually works with no duplicate random number. Why is this?

function shuffle(input) {  
for (let i = input.length - 1; i \>= 0; i–)  
{  
let randomIndex = Math.floor(Math.random() \* (i + 1));  
let itemAtIndex = input[randomIndex];  
input[randomIndex] = input[i];  
input[i] = itemAtIndex;  
}  
return input;  
}

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [July 16, 2022, 3:24pm UTC](https://forum.kirupa.com/t/chapter-19-math-random-im-confused/652661/2 "2022-07-16T15:24:39Z")

</div>

> [@kevin321](#):
>
> When I run the code it actually works with no duplicate random number. Why is this?

What makes you think you aren’t getting duplicates?

Something that’s potentially interesting about this code is that the random number is an index into the array, so swapping items at a random index never means that you get duplicate _items_ in the array, merely the potential for reversed swaps.

It’s sort of like if you’re playing musical chairs with people running around randomly between chairs. Unless you have a machine that clones people, all the randomization in the order that people choose chairs isn’t going to result to duplicate people. The worst thing you can get is an uninteresting shuffle.

---

<div class="post-metadata">

**Author:** ![kevin321](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kevin321/32/18265_2.png) [@kevin321](https://forum.kirupa.com/u/kevin321)\
**Post date:** [July 16, 2022, 3:48pm UTC](https://forum.kirupa.com/t/chapter-19-math-random-im-confused/652661/3 "2022-07-16T15:48:34Z")

</div>

Thanks for your explanation, I get it now. Its just swapping the index based on a random Index number. It was confusing to get my head around it and now I understand it.  
Thank you!

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [July 16, 2022, 3:59pm UTC](https://forum.kirupa.com/t/chapter-19-math-random-im-confused/652661/4 "2022-07-16T15:59:30Z")

</div>

Glad the extra explanation helped; enjoy the rest of the book!

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [July 16, 2022, 6:01pm UTC](https://forum.kirupa.com/t/chapter-19-math-random-im-confused/652661/5 "2022-07-16T18:01:07Z")

</div>

One thing that you may find interesting @kevin321 is this Random Number Frequency Visualizer that shows how often the same number gets hit 😛

> **[Random Number Frequency Visualizer](https://www.kirupa.com/tools/random_number_frequency_viz.htm)**
>
> It's not just enough to generate a random number using Math.random(). You want to ensure the frequency of the generated numbers is fair.
