JavaScript Random Number Method in Easy Way

In this tutorial, we will learn about the JavaScript random number. First, let’s examine the Math object.

How to Make Your JavaScript Code 10x Faster (Proven Techniques 🚀)

What is a Math object?

A built-in static object named “Math” is part of JavaScript.

You can perform mathematical computations with this item. It has a few built-in functions that facilitate mathematical operations.

Using the following technique, we can obtain the random number in JavaScript:

Math.floor() Math.random()
With the aid of examples, we will fully understand these techniques.

Syntax: Math.method_name(number); 

Math.random()

This technique always yields a value less than 1 because it is used to return a random number between 0 and 1.

Syntax: 👉 const num = Math.random();

Example👇

Random number 0 To 1

console.log(Math.random());

Random Integer 1 to 10

let randomNum = Math.floor(Math.random() * 10) + 1;
console.log(randomNum);

OUTPUT-7

Random integer between Two Numbers

function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

console.log(getRandomInt(50, 100)); // Random number between 50 and 100

OUTPUT-78

Random Elements from an array

let colors = ["Red", "Blue", "Green", "Yellow", "Orange"];
let randomColor = colors[Math.floor(Math.random() * colors.length)];
console.log(randomColor);

OUTPUT- GREEN

Random String Generator

function generateRandomString(length) {
    let characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    let result = "";
    for (let i = 0; i < length; i++) {
        result += characters.charAt(Math.floor(Math.random() * characters.length));
    }
    return result;
}

console.log(generateRandomString(8)); // Example: "A1bC2dE3"

Random Hex Color code Generator

function getRandomHexColor() {
    return "#" + Math.floor(Math.random() * 16777215).toString(16);
}

console.log(getRandomHexColor()); // Example: "#f3a9c2"

Random Password generator In JavaScript With Math function

function generatePassword(length) {
    let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()";
    let password = "";
    for (let i = 0; i < length; i++) {
        password += chars.charAt(Math.floor(Math.random() * chars.length));
    }
    return password;
}

console.log(generatePassword(12)); // Example: "P@5sW0rD!xY7"

Random Boolean generator

let randomBool = Math.random() < 0.5;
console.log(randomBool);

Math.floor()

In order to generate a random number, we combine Math.floor() with Math.random(). It is used to round a number down to the closest integer and returns a random number between 0 and 1, meaning that the number is always less than 1.

Basic Example

Rounds down a decimal number to its nearest whole number.

console.log(Math.floor(4.9)); // Output: 4
console.log(Math.floor(7.2)); // Output: 7
console.log(Math.floor(-2.8)); // Output: -3

Random integer 0 to 9

Using Math.floor() and Math.random(), we may obtain a random whole number between 0 and 9.

console.log(Math.floor(Math.random() * 10)); 
// Example output: 3 (varies every time)

Random integer Between a range

Generates a random whole number between the minimum and maximum

function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

console.log(getRandomInt(1, 100)); 
// Example output: 45 (varies every time)

Convert String to Integer in javascript With Math function

console.log(Math.floor("5.99")); // Output: 5
console.log(Math.floor("10.1")); // Output: 10

Floor Division

console.log(Math.floor(10 / 3)); // Output: 3
console.log(Math.floor(25 / 4)); // Output: 6

Date and time calculation Using Math.floor ()

let hours = 50;
let days = Math.floor(hours / 24);
console.log(days); // Output: 2

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *