Home
 
 
Search:  
C C++ Perl PHP Python HTML ShellScripts
 
 
  Coding Books
  Tutorials
  Search Code
  Browse Code
  Link to Us
  Site News
  Contact Metalshell
 
 
 
  Submit Code   Statistics
 



Special Edition Using HTML 4 (6th Edition)    (ISBN: 0789722674)


 

 List Price: $39.99
 Our Price: $27.99
 Used Price: $4.94

 Release Date: 22 December, 1999
 Manufacturer: Que (Paperback)
 Sales Rank: 20,652

 Author: Molly E. Holzschlag









More Info

 JavaScript Random Number Generator 2003-05-27 16:47:35
  html 
Category: source:html:javascript
Description: Generate random numbers, round them to the nearest integer and print them in a select list.
Platform: all
Author: detour
Viewed: 20718
Rating: 3.8/5 (93 votes)
If you have any questions about this piece of code or still need help, try posting your question on the forum.

 

Printable Version
javascript.js
<!-- 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>
Rate this code:
(Not Helpful)  (Very Helpful) 

 
 
   Developer.*  
   Blue Parrots  
   Technipal  
   Defy Magazine  
   Code Project  
   Prog. Heaven  


Got Money?