You are here: irt.org | FAQ | JavaScript | Random | Q287 [ previous next ]
There is an existing JavaScript method that returns a random number:
//Returns a random number between 0 and 1 function getRandom() { return Math.random() }
In Netscape Navigator 2 this only works on Unix platforms, so you might want to try the following instead:
<script> <!-- // The Central Randomizer 1.3 (C) 1997 by Paul Houle (houle@msc.cornell.edu) // See: http://www.msc.cornell.edu/~houle/javascript/randomizer.html rnd.today=new Date(); rnd.seed=rnd.today.getTime(); function rnd() { rnd.seed = (rnd.seed*9301+49297) % 233280; return rnd.seed/(233280.0); }; function rand(number) { return Math.ceil(rnd()*number); }; // end central randomizer. --> </SCRIPT>
To create a random number between 1 and 10 use rand(10). To display an random image named banner1.gif to banner10.gif use:
<script language="JavaScript"><!-- document.write('<img src="banner' + rand(10) + '.gif" width="400" height="40">'); //--></script>