JavaScript Math Functions

Some useful Math functions

In the Calculator Mini-Lab, you used the parseFloat function to convert a string containing a number to the number itself. For example,

    var numVar = parseFloat("5");
converts the string "5" to the number 5 and stores it in the variable numVar.

There is also a Math object in Javascript, with many useful methods. Four of them are shown below. The values these functions manipulate and return are floating point numbers (CS-speak for real numbers), rather than integers. (See the footnote for why they are called "floating point" numbers.)

Math.random() return a random number from 0 - 0.99999...
Math.round(value) round floating point number to nearest integer
Math.floor(value) return nearest integer below floating point value
(like always "rounding" down)
Math.ceil(value) return nearest integer above floating point value
(like always "rounding" up)


Click the button several times to see that you get a different random number each time and to be sure that you understand what each of these functions does. Notice that the rounded value and the floor are sometimes the same and sometimes different. In fact, the rounded value is always the same as either the floor or the ceiling.

The function that is executed when you click this button is:
function showMathExamples()
    var randNumber = Math.random();
    alert("The original random number is: " + randNumber + ".\n" +
            "Rounded, it is: " + Math.round(randNumber) + ".\n" +
            "Its floor is: " + Math.floor(randNumber) + ".\n" +
            "Its ceiling is: " + Math.ceil(randNumber) + ".");

Getting a different range of random numbers

What if you want a random number in a different range, rather than 0 - 0.999999? You can adjust the range by applying basic arithmetic operations to the result — you can use multiplication to widen the range and addition (or subtraction) to shift it. You can also use the Math object's floor and ceil methods to get random integers rather than random floating point numbers.


Assume you have a random number returned by the Math.random() function and another, uninitialized variable:

var randNumber = Math.random();
var newRandNum;

Widening the range: multiply the result by the desired upper bound

For example,

Desired range Adjustment
0 - 2.99999 newRandNum = randNumber * 3;
integer 0 - 2 newRandNum = Math.floor(randNumber * 3);
integer 1 - 3 newRandNum = Math.ceil(randNumber * 3);
integer 1 - 10 newRandNum = Math.ceil(randNumber * 10);
integer 0 - 10 newRandNum = Math.floor(randNumber * 11);
integer 0 - 25 newRandNum = Math.floor(randNumber * 26);
integer 0 - 99 newRandNum = Math.floor(randNumber * 100);

Shifting the range: add (or subtract) the result to shift from 0 to the desired lower bound

For example,

Desired range Adjustment
20 - 20.99999 newRandNum = randNumber + 20;
-1 - 0 (but will never be exactly 0) newRandNum = randNumber - 1;

Doing both

Generally it is easier to multiply the initial random number to get the desired width first, and then shift it.

Desired range Adjustment
20 - 29.99999 (width 10, shifted by 20) newRandNum = (randNumber * 10) + 20;
integer 20 - 99 (width 80, shifted by 20) newRandNum = Math.floor((randNumber * 80) + 20);


Stop and Think: Why wouldn't you use Math.round(randNumber*10) if you want an integer value between 0 and 10 (including both 0 and 10)? What would be the probability of getting 0? Of getting 1? Of getting 10?

Footnote: A computer only has a certain number of binary "digits" ("bits") for storing numbers, so there are many real numbers they can't represent completely accurately; instead, they can only represent approximations. Because of this, and because of the way such numbers are represented internally, computer scientists call them floating point numbers rather than real numbers.