N Random Elements in Array

Beginner

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore how to select random elements from an array using JavaScript. We will learn how to shuffle an array using the Fisher-Yates algorithm and use it to randomly select one or more elements from an array. This lab is designed to enhance your understanding of array manipulation and improve your problem-solving skills in JavaScript.

Code Practice: Getting Random Elements from an Array

To practice coding, open the Terminal/SSH and type node. The following code utilizes the Fisher-Yates algorithm to shuffle an array and retrieve n random, unique elements at unique keys from the array, up to the size of the array.

const sampleSize = ([...arr], n = 1) => {
  let m = arr.length;
  while (m) {
    const i = Math.floor(Math.random() * m--);
    [arr[m], arr[i]] = [arr[i], arr[m]];
  }
  return arr.slice(0, n);
};

To use this code, call sampleSize() with an array and an optional number n of elements to retrieve. If n is not provided, the function will return only one element at random from the array.

sampleSize([1, 2, 3], 2); // [3, 1]
sampleSize([1, 2, 3], 4); // [2, 3, 1]

Summary

Congratulations! You have completed the N Random Elements in Array lab. You can practice more labs in LabEx to improve your skills.