This is the one which is used by many people.  You may not use it but if you ever want to show quotes at random, create a game or create anything which acts differently each time you will need random numbers to power the program flow.  Try this simple example in a new file called random.html:

``````var myRandomNumber=Math.random();

Try it.  Each time you load the page you should get a different number between 0 and 1.  Out of interest you can get 0 but the highest number will be 0.999999 etc..

If you want larger numbers just multiply the result.  You might need to add 1 as well if you want a specific range of 1 to 100 rather than 0 to 99. Use floor to limit the decimal places (or don't add 1 and use ceil instead of floor).  Change the first line of code to:

``var myRandomNumber=Math.floor((Math.random()*100)+1);``

Try it a few times.

This code also illustrates an interesting decision in programming.  That line first creates a random number.  Then it multiplies it by 100.  Then it adds 1.  Then it rounds it down.  Look through and try to follow that.  In one line of code many things are being done but they could be done step by step:

var myRandomNumber=Math.random();

myRandomNumber=myRandomNumber*100;

myRandomNumber=myRandomNumber+1;

myRandomNumber=Math.floor(myRandomNumber);

By using brackets you can put all that on one line and get the same result.  That is obviously more efficient.  However, when the result is wrong which bit of the lien is broken?  With individual lines you can inspect the number at each stage and find out where the mistake is.  So sometimes it pays to write inefficient code.

Adapt the first line in random.html to get random numbers between 1 and 5 and test it a few times to see if those two numbers (and the ones between) do come up.