Here you find common examples of random numbers in javascript.

# Random number examples in javascript.

## Random number from 0.000000... to 0.999999...

Includes 0.0, Excludes 1.0

```Math.random()
0.5653749472719645
Math.random()
0.18129516667898726
Math.random()
0.38332031525383603
Math.random()
0.11096588528106843
Math.random()```

## Random integer from 0 to 9

Includes integer 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9.

```Math.floor(Math.random() * 10)
7
Math.floor(Math.random() * 10)
6
Math.floor(Math.random() * 10)
9
Math.floor(Math.random() * 10)
0
Math.floor(Math.random() * 10)
7```

## Random integer from 1 to 10

Includes integer 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10.

```Math.floor((Math.random() * 10) + 1)
10
Math.floor((Math.random() * 10) + 1)
5
Math.floor((Math.random() * 10) + 1)
6
Math.floor((Math.random() * 10) + 1)
4
Math.floor((Math.random() * 10) + 1)
10
Math.floor((Math.random() * 10) + 1)
1```

## Random integer 0 or 1

Generates the two integers 0 and 1.

```Math.floor((Math.random() * 2))
1
Math.floor((Math.random() * 2))
1
Math.floor((Math.random() * 2))
0
Math.floor((Math.random() * 2))
0
Math.floor((Math.random() * 2))
0
Math.floor((Math.random() * 2))
1
Math.floor((Math.random() * 2))
0```

## Random boolean

Use the double equals operator to get the truthy value from 0 and 1:

```Math.floor((Math.random() * 2)) == true
false
Math.floor((Math.random() * 2)) == true
false
Math.floor((Math.random() * 2)) == true
true
Math.floor((Math.random() * 2)) == true
true
Math.floor((Math.random() * 2)) == true
false```

## Random integer between two integer values

Get a random integer from (including) a minimum integer to (including) a max integer.

This code generates integers in range: 2, 3, 4, 5, 6 and 7

```const min = 2;
const max = 7;
Math.floor((Math.random() * (max - min + 1)) + min)
4
Math.floor((Math.random() * (max - min + 1)) + min)
2
Math.floor((Math.random() * (max - min + 1)) + min)
6
Math.floor((Math.random() * (max - min + 1)) + min)
7
Math.floor((Math.random() * (max - min + 1)) + min)
3
Math.floor((Math.random() * (max - min + 1)) + min)
4```
Written by Loek van den Ouweland on 2021-08-16.
Questions regarding this artice? You can send them to the address below.