JS: Random Range Function

By Xah Lee. Date: . Last updated: .

How to generate a random number?

Math.random() → return a a pseudo-random number between 0 to 1. (including 0 but excluding 1)

[see JS: Math]

How to get a random real number between min and max?

// es2015
// 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));

How to get a random integer?

// es2015
// 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.

How to seed random number?

You can't.

Random Array Element, Randomize Array

JS: Random Array Element, Randomize Array

JS Number

  1. Format Number
  2. String to Number
  3. Random Number

Like it? Help me by telling your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials