# JS: Random Range Function

By Xah Lee. Date: . Last updated: .

## How to generate a random number?

```// generate a pseudo-random number between 0 to 1. (including 0 but excluding 1)

Math.random();```

[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));```

Here's pre es2015 version:

```// pre es2015

var xah_random_real = function (xmin,xmax) {
// random real number in range {min, max}, including min but excluding max.
// version 2017-05-12
return 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 ))) ;```

Here's pre es2015 version:

```// pre es2015
var xah_random_int = function (xmin,xmax) {
// random integer in range {min, max}, including min and max
return Math.floor( Math.random() * (xmax + 1 - xmin) + xmin );
};```

## How is random number seeded?

It's based on current time.

You can't.

## Random Array Element, Randomize Array

JS: Random Array Element, Randomize Array

Liket it? I spend 2 years writing this tutorial. Help me spread it. Tell your friends. Or, Put \$5 at patreon.

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