--------------------------------------------------- | Date: 2003-05-27 16:47:35 | | Filename: javascript.js | | Author: detour@metalshell.com | | | | http://www.metalshell.com/ | --------------------------------------------------- <!-- random.js by detour@metalshell.com --> <!-- Generate 100 random numbers, round and print them. --> <!-- http://www.metalshell.com/ --> <html> <head> <title>JavaScript Random Number Generator.</title> </head> <body> <script type="text/javascript"> // Generate 100 random numbers and insert them into listbox function genNumbers(listbox) { var i, rannum; for(i = 0; i < 100; i++) { // Math.random() returns a decimal number between // 0 and 1. You multiple the result by the range // of numbers you want. // This will generate 0-100. rannum = Math.random()*100; // This would generate 145-200 // rannum = Math.random()*55 + 145; // rannum is now a decimal value, to turn it into // an integer use Math.round to round it to the nearest // integer. Math.floor(rannum) to always round down and // Math.ceil(rannum) to round up. rannum = Math.round(rannum); // If options[i] hasn't been created yet, create it. if(listbox.options[i] == null) { listbox.options[i] = new Option( rannum, rannum, 0, 0 ); } else { listbox.options[i].value = rannum; listbox.options[i].text = rannum; } } } </script> <form> <select name="ranlist" size="20" style="width:200px"> </select><br><br> <input type="button" value="Generate Numbers" onclick="genNumbers(this.form.ranlist)";> </form> </body> </html>