How do you generate a random number in javascript?

The Math.random[] function returns a floating-point, pseudo-random number that's greater than or equal to 0 and less than 1, with approximately uniform distribution over that range — which you can then scale to your desired range. The implementation selects the initial seed to the random number generation algorithm; it cannot be chosen or reset by the user.

Try it

Note: Math.random[] does not provide cryptographically secure random numbers. Do not use them for anything related to security. Use the Web Crypto API instead, and more precisely the window.crypto.getRandomValues[] method.

Syntax

Return value

A floating-point, pseudo-random number between 0 [inclusive] and 1 [exclusive].

Examples

Note that as numbers in JavaScript are IEEE 754 floating point numbers with round-to-nearest-even behavior, the ranges claimed for the functions below [excluding the one for Math.random[] itself] aren't exact. If extremely large bounds are chosen [253 or higher], it's possible in extremely rare cases to reach the usually-excluded upper bound.

Getting a random number between 0 [inclusive] and 1 [exclusive]

function getRandom[] {
  return Math.random[];
}

Getting a random number between two values

This example returns a random number between the specified values. The returned value is no lower than [and may possibly equal] min, and is less than [and not equal] max.

function getRandomArbitrary[min, max] {
  return Math.random[] * [max - min] + min;
}

Getting a random integer between two values

This example returns a random integer between the specified values. The value is no lower than min [or the next integer greater than min if min isn't an integer], and is less than [but not equal to] max.

function getRandomInt[min, max] {
  min = Math.ceil[min];
  max = Math.floor[max];
  return Math.floor[Math.random[] * [max - min] + min]; // The maximum is exclusive and the minimum is inclusive
}

Note: It might be tempting to use Math.round[] to accomplish that, but doing so would cause your random numbers to follow a non-uniform distribution, which may not be acceptable for your needs.

Getting a random integer between two values, inclusive

While the getRandomInt[] function above is inclusive at the minimum, it's exclusive at the maximum. What if you need the results to be inclusive at both the minimum and the maximum? The getRandomIntInclusive[] function below accomplishes that.

function getRandomIntInclusive[min, max] {
  min = Math.ceil[min];
  max = Math.floor[max];
  return Math.floor[Math.random[] * [max - min + 1] + min]; // The maximum is inclusive and the minimum is inclusive
}

Specifications

Specification
ECMAScript Language Specification
# sec-math.random

Browser compatibility

BCD tables only load in the browser

See also

Examples

let x = Math.random[];

Try it Yourself »

Return a random number between 0 and 10:

let x = Math.random[] * 10;

Try it Yourself »

Return a random number between 0 and 100:

let x = Math.random[] * 100];

Try it Yourself »

A random whole number between 1 and 10:

let x = Math.floor[[Math.random[] * 10] + 1];

Try it Yourself »

A random whole number between 1 and 100:

let x = Math.floor[[Math.random[] * 100] + 1];

Try it Yourself »

Definition and Usage

The Math.random[] method returns a random number from 0 [inclusive] up to but not including 1 [exclusive].

Note

Math.random[] does not return a cryptographically secure number.

If you need a cryptographically secure number, use this Crypto API method:
crypto.getRandomValues[]

Syntax

Parameters

Return Value

Type Description
Number A random number from 0 [inclusive] up to but not including 1 [exclusive].

Browser Support

Math.random[] is an ECMAScript1 [ES1] feature.

ES1 [JavaScript 1997] is fully supported in all browsers:

Chrome IE Edge Firefox Safari Opera
Yes Yes Yes Yes Yes Yes


How do you generate random numbers in a script?

Random integer can be generated by directly reading from the random variable by using the echo command. It will display a random integer in range 0 to 32767. You may use loops to generate more than one random number using the RANDOM shell variable.

How does JavaScript generate 5 random numbers?

“generate 5 unique random numbers javascript” Code Answer.
var arr = [];.
while[arr. length < 8]{.
var r = Math. floor[Math. random[] * 100] + 1;.
if[arr. indexOf[r] === -1] arr. push[r];.
console. log[arr];.

Is there a random function in JavaScript?

In JavaScript, random[] is a function that is used to return a pseudo-random number or random number within a range. Because the random[] function is a static function of the Math object, it must be invoked through the placeholder object called Math.

How do you generate a random number between two values in JavaScript?

[Math. random[] * 6 | 0] + 1. ~~[Math. random[] * 6] + 1.

Chủ Đề