JavaScript: Random Numbers

 

Math Object

The Math object is used when complex mathematical functions are needed. To store the value of pi (3.14159...) in the variable 'piValue,' for example, you could write:

var piValue = Math.PI

To take the square root of a variable 'X' and store it in 'Xroot,' you could write:

var xRoot = Math.sqrt(X)

 

Random Numbers

To generate a random number between 0 and 1 (including 0 and 1) and store it in 'number,' try:

number = Math.random()

To generate a number between 1 and 10, you would need to multiply Math.random() by 9, and then add 1. Multiplying by 9 generates a number between 0 and 9, and adding 1 yields a number between 1 and 10:

number2 = (Math.random() * 9) + 1

The result 'number2' has a decimal, while you probably want an integer. To get an integer, apply the Math.round() function to 'number2':

number3 = Math.round(number2)

Or combine both steps:

number3 = Math.round(Math.random() * 9 + 1)

 

Example

Let's do an example in which we generate 4 random numbers, similar to what we did above. The ultimate goal is a random number between 1 and 100. Here is the code:

<BODY>

<script language=JavaScript>

<!--

document.write("<em style=font-size:18pt>The following numbers result from 4 separate calls to Math.random(): </em><br><p> " )

number = Math.random()

document.write("The result of Math.random(): ")

document.write("<strong style=font-size:16pt>" + number + "<br><p> </strong>")

number2 = Math.random() * 99

document.write("The result of Math.random() * 99: ")

document.write("<strong style=font-size:16pt>" + number2 + "<br><p> </strong>")

number3 = Math.round(Math.random() * 99)

document.write("The result of Math.round(Math.random() * 99): ")

document.write("<strong style=font-size:16pt>" + number3 + "<br><p> </strong>")

number4 = Math.round(Math.random() * 99) + 1

document.write("The result of Math.round(Math.random() * 99) + 1: ")

document.write("<strong style=font-size:16pt>" + number4 + "<br><p> </strong>")

document.write("<p><em style=font-size:18pt>Hit Refresh or Reload to generate fresh numbers.")

//-->

</script>

 

to see this how this code looks in a new window.

There are a few things to note about the above script. To use the variables 'number,' 'number2,' 'number3' and 'number4' we didn't start with the keyword 'var.' You don't always have to begin with 'var;' it is used in functions where you are declaring local variables (more on this later.) The use of 'var' is optional elsewhere.

Note, as we saw earlier, that HTML and style definitions can be included inside the document.write() command. These elements can co-exist with variables in document.write().

 

Other Resources

Try these links on the Math object:


Page Link
Numbers www.webreference.com/js/tips/browse.html?cat=nu&start=1

or check out one of our reference books, or look for other help on the Web.

 

Exercise

Work on either 1) your own web page, or 2) the eEverything.com site. If you work on eEverything.com, create a piece of code that uses the random() function to find the winning numbers for the daily Instant Money Grab game. The winning number is composed of 2 numbers between 1 and 500 (like "231 17").

If you work on your own site, implement something similar.