back
Die 1 is:
Die 2 is:
Dice total:

. . .

<script. . .>

function rollDice()

{

    var d1 = Math.ceil(Math.random() * 6)

    var d2 = Math.ceil(Math.random() * 6)

    var tot = d1 + d2

    document.form1.die1.value = d1

    document.form1.die2.value = d2

    document.form1.total.value = tot

    document.images[0].src="images/dice_" + d1 + ".gif"

    document.images[1].src="images/dice_" + d2 + ".gif"

 }

</script>

 . . .

<body>

   <form name="form1">

    Die 1 is: <input Type="text" name="die1" SIZE="1">

    <IMG src="images/dice_1.gif" ALIGN="center" WIDTH="20" HEIGHT="20" ><br>

    Die 2 is: <input Type="text" name="die2" SIZE="1">

    <IMG src="images/dice_1.gif" ALIGN="center" WIDTH="20" HEIGHT="20" ><br>

    Dice total: <input Type="text" name="total" SIZE="2"> <br>

    <input type="button" value="Roll Dice" onClick="rollDice()">

  </form>

. . .

Pick A Number from 4 to 6
Computer guess:
Total correct: out of: Pct Correct: %
======================================= Random Numbers ======================================= Definition and Usage The random() method returns a random number between 0 and 1. Note: JavaScript's random() is a Pseudorandom Number Syntax Math.random() A simple example Random thow of the Dice - - - - - - - - - - - - - - - - - - - - - Seeding - - - - - - - - - - - - - - - - - - - - - random() Returns a pseudo-random number between zero and one. In JavaScript a random number generator is from current time, as in Java and other complex computer languages This seed is the number that is run through the randomization algorithm. You can also create your own seed by entering a value as a parameter. Math.random(5) however, 5 just runs the number 5 through the algorithm. Seeding with a constant number, or no seed number at all, can lead to recurring results. This should be avoided as the whole purpose of randomization is to produce unpredictable results. To get around seeding with a constant number you can use a constantly changing number to seed your "Math.random()". One favorite is the seconds from the Date()object. now = new Date(); seed = now.getSeconds(); var random_number = Math.random(seed); - - - - - - - - - - - - - - - - - - - - - Setting a Range - - - - - - - - - - - - - - - - - - - - - If you want your random number to be a whole number you need to build a range. For example, if you add the range below, you will produce a number from 0-9 var range = random_number *10; add a range to the code above now = new Date(); seed = now.getSeconds(); var random_number = Math.random(seed); var range = random_number *10; If you want a range between two numbers take the difference between the two numbers as the range and add the value of the returned random number to the low end of the range you want it to fall between now = new Date(); seed = now.getSeconds(); var random_number = Math.random(seed); var range = Math.floor(random_number * 3); range += 4 Example Pick a number from 4 to 6 (ie., 4, 5, or 6)

function pick_a_number_from_4_to_6()

 {

  now = new Date();

  seed = now.getSeconds();

  var random_number = Math.random(seed);

  var range = Math.floor(random_number * 3);     // val from 4 to 6 (ie., 4, 5, and 6)

        range += 4                                                      // this is the low end of the range

  document.form2.CPU_Guess.value = range

  if(document.form2.CPU_Guess.value == document.form2.User_Guess.value)

  {

   document.form2.correct.value =  parseInt(document.form2.correct.value) + 1

   document.form2.Total.value =   parseInt(document.form2.Total.value) + 1

   document.form2.PctCorrect.value = parseFloat((document.form2.correct.value / document.form2.Total.value)*100).toFixed(2); 

  }

  else

  {

   document.form2.Total.value =  parseInt(document.form2.Total.value) + 1

   document.form2.PctCorrect.value =  parseFloat((document.form2.correct.value / document.form2.Total.value)*100).toFixed(2);

 }

 

. . .

 

  <form name="form2">

    Pick A Number from 4 to 6 <input Type="text" name="User_Guess" SIZE="1">

     <br>

    Computer guess: <input Type="text" name="CPU_Guess" SIZE="1">

     <br>

    Total correct: <input Type="text" name="correct" SIZE="2" value=0>

            out of: <input Type="text" name="Total" SIZE="2" value=0>

            Pct Correct: <input Type="text" name="PctCorrect" SIZE="2" value=0>%<br>

    <input type="button" value="Is your guess the same as the Computer"

                onClick=" pick_a_number_from_4_to_6()">

  </form>

view the source of this document.