# Using Math random() function to coding in Javascript/JS

This JavaScript tutorial explains how to use the math function referred to as random() with syntax and examples.

## Description

In JavaScript, random() is a function that is used to return a pseudo-random quantity or random variety within a range. Because the random() characteristic is a static function of the Math object, it should be invoked thru the placeholder object referred to as Math.

## Syntax

In JavaScript, the syntax for the random() feature is:

``Math.random();``

Parameters or Arguments

There are no parameters or arguments for the random() function.

## Returns

The random() characteristic returns a price between zero (inclusive) and 1 (exclusive), so price >= zero and cost < 1.

## Note

Math is a placeholder object that carries mathematical features and constants of which random() is one of these functions.

## Example

Let’s take a appear at an instance of how to use the random() characteristic in JavaScript.

For example:

``console.log(Math.random());``

In this example, we have invoked the random() characteristic the usage of the Math class.

We have written the output of the random() characteristic to the web browser console log, for demonstration purposes, to show what the random() characteristic returns.

The following will be output to the internet browser console log:

``0.0390260436146006``

In this example, the first output to the console log returned 0.0390260436146006 which is a random range >= 0 and < 1. (you will most likely see a special end result from the random() feature and now not the cost 0.0390260436146006).

Random Decimal Range

To create a random decimal wide variety between two values (range), you can use the following formula:

``Math.random()*(b-a)+a;``

Where a is the smallest quantity and b is the greatest number that you choose to generate a random number for.

``console.log(Math.random()*(25-10)+10);``

The method above would generate a random decimal range >= 10 and < 25. (Note: this formula will in no way return a fee of 25 because the random characteristic will in no way return 1.)

The following will be output to the net browser console log:

``11.94632888346256``

The cost 11.94632888346256 is a decimal range between 10 (inclusive) and 25 (exclusive). (Note: the result you get will be distinct due to the fact the random() function returns a random number)

Random Integer Range

To create a random integer wide variety between two values (inclusive range), you can use the following formula:

``Math.floor(Math.random()*(b-a+1))+a;``

Where a is the smallest variety and b is the biggest wide variety that you desire to generate a random variety for.

``console.log(Math.floor(Math.random()*(25-10+1))+10);``

The components above would generate a random integer variety between 10 and 25, inclusive.

The following will be output to the internet browser console log:

``15``

The value 15 is an integer number between 10 (inclusive) and 25 (inclusive). (Note: the result you get will be one of a kind because the random() characteristic returns a random number)