<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sagdi Formanov</title>
    <description>The latest articles on DEV Community by Sagdi Formanov (@sagdish).</description>
    <link>https://dev.to/sagdish</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F413942%2Fd1bd04e8-6d23-4c0b-8556-82e26ec9cddb.jpeg</url>
      <title>DEV Community: Sagdi Formanov</title>
      <link>https://dev.to/sagdish</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sagdish"/>
    <language>en</language>
    <item>
      <title>Generate unique (non-repeating) random numbers</title>
      <dc:creator>Sagdi Formanov</dc:creator>
      <pubDate>Mon, 23 Nov 2020 02:10:03 +0000</pubDate>
      <link>https://dev.to/sagdish/generate-unique-non-repeating-random-numbers-g6g</link>
      <guid>https://dev.to/sagdish/generate-unique-non-repeating-random-numbers-g6g</guid>
      <description>&lt;p&gt;&lt;strong&gt;Backstory:&lt;/strong&gt; when I was doing one of my side projects, one of the tasks was to display photos from Unspash API in multiple cards. Goal was to pick photos randomly without repeating them. I used good old &lt;code&gt;Math.random()&lt;/code&gt; to randomly pick photos from API's response. But the problem was that often few numbers were repeating, thus there were the same pictures in different cards.&lt;/p&gt;

&lt;p&gt;Let's say you want to generate 5 random unique numbers from 1 to 10. Big chances that you'll get at least one repeated number.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Solution for this task is to replace each picked (random) number in array with another unused one.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In the code this would be something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function randomUniqueNum(range, outputCount) {

  let arr = []
  for (let i = 1; i &amp;lt;= range; i++) {
    arr.push(i)
  }

  let result = [];

  for (let i = 1; i &amp;lt;= outputCount; i++) {
    const random = Math.floor(Math.random() * (range - i));
    result.push(arr[random]);
    arr[random] = arr[range - i];
  }

  return result;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's look line by line.&lt;br&gt;
Function takes range, and output count.&lt;br&gt;
For instance &lt;code&gt;randomUniqueNum(10, 5)&lt;/code&gt;&lt;br&gt;
First we generate array from 1 to 10&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; let arr = []
  for (let i = 1; i &amp;lt;= 10; i++) {
    arr.push(i)
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then we use another loop to pick numbers from this array.&lt;br&gt;
Iterations in this loop equals to output count, in this case to 5.&lt;br&gt;
Then we generate random number:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const random = Math.floor(Math.random() * (range - i))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each iteration we decreasing range by 1.&lt;br&gt;
Then using this random number as index in &lt;code&gt;arr&lt;/code&gt; we push it to the result array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;result.push(arr[random])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that we replace 'used' number in &lt;code&gt;arr&lt;/code&gt; with the one from the end of the same array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr[random] = arr[range - i]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though in the next iteration &lt;code&gt;Math.random()&lt;/code&gt; will give us the same number, we'll get different result because we replaced it with the number from the end.&lt;/p&gt;

&lt;p&gt;Since we decrease range in each iteration: &lt;code&gt;range - i&lt;/code&gt; so numbers from upper end of array will not be picked.&lt;br&gt;
At the end we just return array of unique random numbers.&lt;/p&gt;

&lt;p&gt;I hope this would help you in any way.&lt;/p&gt;

</description>
      <category>random</category>
      <category>unique</category>
      <category>numbers</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
