BuiltWithNOF
Ex. 13.8

<html>
 <!-- stats.html                               Dave Reed -->
 <!-- This page simulates dice rolls and maintains stats. -->
 <!--------------------------------------------------------->

<head>
  <title> Dice Stats </title>
  <script type="text/javascript"
         src="http://www.prenhall.com/reed/random.js">
  </script>
  <script type="text/javascript">
     function RollRepeatedly()
     // Assumes: document.DiceForm.reps contains a number
     // Results: simulates that many dice rolls, displays # of doubles
     {
       var doubleCount, totalRolls, repCount, roll1, roll2;

       doubleCount = 0;                         // initialize the counter
       totalRolls = parseFloat(document.DiceForm.reps.value);

       repCount = 0;
       while (repCount < totalRolls) {          // repeatedly:
           roll1 = RandomInt(1, 6);             // simulate the dice rolls
           roll2 = RandomInt(1, 6);

           if (roll1 == roll2) {                // if doubles,
               doubleCount = doubleCount + 1;   //   increment the counter
           }

           repCount = repCount + 1;             // incr. repetition counter
       }

       document.DiceForm.count.value = doubleCount; // display # doubles
     }
  </script>
</head>

<body>
  <div style="text-align:center">
   <h2>Dice Stats</h2>

   <form name="DiceForm">
   Desired number of rolls =
   <input type="text" name="reps" size=6 value=1000 />
   <br /><br />
   <input type="button" value="Click to Roll" onClick="RollRepeatedly();" />
   <br /><br />
   Number of doubles obtained =
   <input type="text" name="count" size=6 value=0 />
   </form>
  </div>
</body>
</html>
 

[Home] [Syllabus] [Sessions] [Writing Code] [Help] [Setup] [Instructor]