Favorites

JavaScript Math.random() Method

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Math.random()
JavaScript Math Static methodThis method is supported by all modern browsers.
Updated 28th November 2026

The Math.random() static method returns a pseudo-random decimal number from 0 up to (but not including) 1. Scale and round it to get random integers.

Description

The Math.random() static method returns a pseudo-random floating point number in the range 0 to less than 1, with roughly even distribution. To get a random integer between min and max inclusive, use Math.floor(Math.random() * (max - min + 1)) + min. To pick a random array element, use arr[Math.floor(Math.random() * arr.length)].

It's not suitable for anything security-related, such as passwords or tokens, because its output can be predicted. Use crypto.getRandomValues() or crypto.randomUUID() instead.

Each call returns a new, independent number, and there's no way to set a seed to get the same sequence again; for reproducible random numbers, such as in tests or procedural generation, use a small seeded generator function instead.

Syntax

Math.random()

Return Value

A pseudo-random number from 0 (inclusive) up to but not including 1.

Example

console.log(Math.random());

const randomInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
console.log('Dice:', randomInt(1, 6));

const colors = ['red', 'green', 'blue'];
console.log(colors[Math.floor(Math.random() * colors.length)]);

Browser Support

The following information will show you the current browser support for the Math.random() method. Hover over a browser icon to see the version that first introduced support for this method.

This method is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee