JavaScript: Random Number Function

By Xah Lee. Date: . Last updated: .
Math.random()
Return a a pseudo-random (float) number between 0 to 1. (including 0 but excluding 1)

[see Math]

Get a random real number between min and max

/* [ random real number in range {min, max}, including min but excluding max. version 2017-05-12 ] */
const xah_random_real = ((xmin,xmax) => ( Math.random() * (xmax - xmin) + xmin));

Get a random integer

/* [ random integer in range {min, max}, including min and max ] */
const xah_random_int = ((xmin,xmax) => (Math.floor( Math.random() * (xmax + 1 - xmin) + xmin )));

How is random number seeded

It's based on current time.

Seed random number

You can't.

Random Array Element, Randomize Array

Random Array Element, Randomize Array

JavaScript Number

βˆ‘ JS in Depth
XAH Β BUY NOW

JS in Depth

JS Obj Ref

DOM


JS in Depth

Basic Syntax

Value Types

Variable

String

Function

Property

Object and Inheritance

Array

Constructor/Class

Iterable 🌟

Misc