title: How to select a number of random files from a given path using Node.js?
body: <p>I am trying to select a number of random files from a given directory. Below is my current implementation; however, there are too many files inside the folder iterating them all and then pick few random ones seems overkill. </p>
  
  <p>Is there a better solution? Because what I am thinking is knowing all the files inside the folder is the precondition for random selection?</p>
  
  <pre><code>    const dirs = fs.readdirSync(IMAGE_BANK_SRC)
            .map(file =&gt; {
                return path.join(IMAGE_BANK_SRC, file);
            });
  
      const srcs_dup = [];
  
      dirs.forEach(path =&gt; {
          fs.readdirSync(path).forEach(file =&gt; {
              srcs_dup.push(file);
          });
      });
  
      // Pick few random ones from `srcs_dup`
  </code></pre>
  
  <p><strong>Requirements</strong>:</p>
  
  <ol>
  <li>The selected random files should be unique</li>
  <li>The code is still working if the folder contains less files than expected</li>
  <li>As fast as possible</li>
  </ol>