# Math.random Examples

The general syntax for this method is:

``````   Math.random()
``````

This method returns a random decimal value between 0 and 1, which can be stored in a variable or used in a calculation.

Note that 0 is a possible selection, but 1 is NOT.

## Generate a Random Integer

If a random integer must be generated, the result of `Math.random()` can be manipulated with operators (`+, -, *, /`) and other `Math` methods.

The trick to creating a random integer is to multiply `Math.random()` by a whole number and then round the result to remove the decimal portion. The choice of using the `ceil`, `floor`, or `round` method affects the numbers generated.

Explore the example below:

After multiplying `Math.random()` by 10, applying the `floor` method gives numbers between 0 and 9. Using the `ceil` method shifts the range up one digit, generating values between 1 and 10. Using the `round` method gives the widest range, generating numbers between 0 and 10.

Rather than trying to remember which method to use, one choice is to ALWAYS use `floor` to round to an integer:

1. `Math.floor(Math.random()*10)` generates a number from 0 - 9.
2. `Math.floor(Math.random()*120)` generates a number from 0 - 119.

To start our range at 1, just add 1 to the rounded value:

1. `Math.floor(Math.random()*10) + 1` generates a number from 1 - 10.
2. `Math.floor(Math.random()*120) + 1` generates a number from 1 - 120.

By changing the value that multiplies `Math.random()` we specify the range for the numbers we want to generate.

1. `Math.floor(Math.random()*maxValue)` generates a number from 0 to (`maxValue`-1).
2. `Math.floor(Math.random()*maxValue) + 1` generates a number from 1 to `maxValue`.