The math object in JavaScript does not have constructor so we cannot instantiate it. All the methods and properties can be assessed directly from the math object.

Math Object Properties

The following properties are available on the math object

E Returns Euler’s number (approx. 2.718)
LN2 Returns the natural logarithm of 2 (approx. 0.693)
LN10 Returns the natural logarithm of 10 (approx. 2.302)
LOG2E Returns the base-2 logarithm of E (approx. 1.442)
LOG10E Returns the base-10 logarithm of E (approx. 0.434)
PI Returns PI (approx. 3.14)
SQRT1_2 Returns the square root of 1/2 (approx. 0.707)
SQRT2 Returns the square root of 2 (approx. 1.414)

 

Math object methods

The following methods are available on the math object.

abs(x) Returns the absolute value of x
acos(x) Returns the arccosine of x, in radians
asin(x) Returns the arcsine of x, in radians
atan(x) Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians
atan2(y,x) Returns the arctangent of the quotient of its arguments
ceil(x) Returns x, rounded upwards to the nearest integer
cos(x) Returns the cosine of x (x is in radians)
exp(x) Returns the value of Ex
floor(x) Returns x, rounded downwards to the nearest integer
log(x) Returns the natural logarithm (base E) of x
max(x,y,z,…,n) Returns the number with the highest value
min(x,y,z,…,n) Returns the number with the lowest value
pow(x,y) Returns the value of x to the power of y
random() Returns a random number between 0 and 1
round(x) Rounds x to the nearest integer
sin(x) Returns the sine of x (x is in radians)
sqrt(x) Returns the square root of x
tan(x) Returns the tangent of an angle

Generating random number

We have a random method available in the Math class which can be used for the random number generation. The only restriction with this approach is that that the random method outputs a value between 0 and 1 which is not suitable with most of our needs.

function randomBetween0and1() {

console.log("Random number - " + Math.random()); // 0.6579966396093369

console.log("Another random number - " + Math.random()); // 0.5774516856763512

console.log("Yet another random number - " + Math.random()); // 0.7963233035989106

}

To increase the range of our randoms we can simply multiply then every time by the maximum value that we want for our random number. Let’s say we want random number between 0 and 999, so will multiply the output of random function by 999.

function randomBetween0and999Float() {

console.log("a random between 0 and 999 is " + (Math.random() * 999)); // 541.2334246176761

console.log("a random between 0 and 999 is " + (Math.random() * 999)); // 56.533659977372736

console.log("a random between 0 and 999 is " + (Math.random() * 999)); // 889.5993133583106

}

As you can see we got random numbers between 0 and 999 but these random number are not whole numbers to fix that all we need to do is use the floor function available in the math class which rounds of the decimals to nearest value.

function randomBetween0and999() {

console.log("a random between 0 and 999 is " + Math.floor((Math.random() * 999))); // 541

console.log("a random between 0 and 999 is " + Math.floor((Math.random() * 999))); // 56

console.log("a random between 0 and 999 is " + Math.floor((Math.random() * 999))); // 889

}

Now say we want to generate the random number between 99 and 999. We will use the same way but now instead of just multiplying the random number by upper range but we will multiply by upper range – (lower range – 1) and then multiply by lower range.

function randomBetween99and999() {

console.log("a random between 99 and 999 is " + (Math.floor((Math.random() * 901)) + 99)); // 541

console.log("a random between 99 and 999 is " + (Math.floor((Math.random() * 901)) + 99)); // 156

console.log("a random between 99 and 999 is " + (Math.floor((Math.random() * 901)) + 99)); // 889

}

The code for this post can be found here.

Any questions, comments or feedback is really appreciated.