## Spotting patterns

# Random number in javascript

The following snippet creates an integer (no decimals) between the range you choose starting from 1.

`var range = whatever number you want;`

`var randomNumber = Math.floor(Math.random() * range ) + 1;`

This means that if want a number between 1 and 6:

`var range = 6`

or

`var randomNumber = Math.floor(Math.random() * 6) + 1;`

### Explanation

Because Math.random will output a decimal under between 0 and 1 (never will be 1) we round it down, this means that `Math.floor(0.5) = 0`

. We simply multiply the inside by the top number and add to the outside 1 to make it an integer between the specified range.