JavaScript Random

The `Math.random()` function in JavaScript is used to generate pseudo-random numbers. This page covers how to use `Math.random()` and related functions to generate random values and perform tasks like random selection and shuffling.

1. Generating Random Numbers

The `Math.random()` method returns a pseudo-random floating-point number between 0 (inclusive) and 1 (exclusive). This number can be used in various scenarios where random values are needed.

- Basic Example

Generate a random number between 0 and 1:


// Example of Math.random()
console.log(Math.random()); // e.g., 0.732549162800805

            

2. Generating Random Integers

To generate a random integer within a specific range, you can use `Math.random()` in conjunction with other Math methods like `Math.floor()` or `Math.ceil()`.

- Random Integer Between Two Values

Here's how you can generate a random integer between a minimum (inclusive) and maximum (exclusive) value:


// Function to get a random integer between min (inclusive) and max (exclusive)
function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min)) + min;
}

console.log(getRandomInt(1, 10)); // e.g., 7

            

- Random Integer Within a Range

For generating random integers within a specified range, including both bounds:


// Function to get a random integer between min (inclusive) and max (inclusive)
function getRandomIntInclusive(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

console.log(getRandomIntInclusive(1, 10)); // e.g., 4

            

3. Generating Random Floating-Point Numbers

In addition to integers, you can generate random floating-point numbers within a specific range:

- Random Floating-Point Number Between Two Values

Here's how to generate a random floating-point number between a minimum and maximum value:


// Function to get a random floating-point number between min (inclusive) and max (exclusive)
function getRandomFloat(min, max) {
    return Math.random() * (max - min) + min;
}

console.log(getRandomFloat(1.5, 5.5)); // e.g., 3.727

            

4. Random Selection from an Array

You can use random numbers to select random items from an array:

- Random Item from Array

Here’s a function to select a random item from an array:


// Function to select a random item from an array
function getRandomElement(arr) {
    return arr[Math.floor(Math.random() * arr.length)];
}

const fruits = ['apple', 'banana', 'cherry', 'date'];
console.log(getRandomElement(fruits)); // e.g., 'cherry'

            

5. Randomizing an Array (Shuffling)

Shuffling an array involves randomizing the order of its elements. The Fisher-Yates (or Knuth) shuffle algorithm is commonly used for this purpose:

- Fisher-Yates Shuffle Algorithm

Here's how to shuffle an array using the Fisher-Yates algorithm:


// Function to shuffle an array using the Fisher-Yates algorithm
function shuffleArray(array) {
    for (let i = array.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [array[i], array[j]] = [array[j], array[i]]; // Swap elements
    }
    return array;
}

const numbers = [1, 2, 3, 4, 5];
console.log(shuffleArray(numbers)); // e.g., [3, 5, 1, 4, 2]