Making random numbers  

  • A common computer application:

    • Computer simulation

  • Problems/applications that use computer simulation:

    • Video games
    • System analysis

  • How computer simulation is used:

    • We build a computer program that simulates the actual operation of a real world system

    • Then we run the simulation program many times using different input patterns and study the behavior of the system

      (Different input patterns may result in different system behavior)

  Making random numbers  

  • Computer simulation requires:

    • Generating random numbers

  • The Math library class in Java contains a random number generation method:

     Math.random()     returns a random number between [0.0  ..  1.0)
    

    Example:

       public static void main(String[] args) 
       {
          double x;
          
          x = Math.random();  // Math.random() returns
          x = Math.random();  // a different number 
          x = Math.random();  // each time it is invoked
       } 

DEMO: demo/03-selections/05-random/Random1.java

  Making random numbers between [0 .. M) 

  • Math.random() only return random numbers between [0.0 .. 1.0)

  • We can generate random numbers between [0.0 .. M) by scaling the numbers in the range [0.0 .. 1.0):

    • multiplying by M

       

       

    Example: generating random numbers between [0.0 .. 6.0):

       public static void main(String[] args) 
       {
          double x;
          
          x = 6.0 * Math.random(); 
          x = 6.0 * Math.random(); 
          x = 6.0 * Math.random();
       } 

DEMO: demo/03-selections/05-random/Random2.java

  Making random numbers between [k .. M+k)  

  • According to the previous slide:

    • M * Math.random() will return random numbers between [0.0 .. M.0)

  • We can generate random numbers between [k .. M+k) by shifting the numbers in the range [0.0 .. M.0):

    • adding k  

       

       

    Example: generating random numbers between [1.0 .. 7.0):

       public static void main(String[] args) 
       {
          double x;
          
          x = 6.0 * Math.random() + 1.0; 
          x = 6.0 * Math.random() + 1.0; 
          x = 6.0 * Math.random() + 1.0;
       } 

DEMO: demo/03-selections/05-random/Random3.java

  Making integer random numbers in the range [k,2,3,4, ..., M+k] 

  • According to the previous slide:

    • M * Math.random() + k return random numbers between [k .. M+k)

  • We can generate integer random numbers between [k .. M+k-1] by truncating the random number in the range [k .. M+k):

    • Convert double to int

       

    Example: generating random numbers between [1 .. 6]:

       public static void main(String[] args) 
       {
          int k;
          
          k = (int) (6.0 * Math.random() + 1.0); 
          k = (int) (6.0 * Math.random() + 1.0); 
          k = (int) (6.0 * Math.random() + 1.0); 
       } 

DEMO: demo/03-selections/05-random/Random4.java

Using random numbers to make subtraction practice questions for 1st graders

Write the following Java program:

  • Suppose you want to write a Java program to help a first-grader to practice subtraction.

  • The program generates two single-digit (between 1-9) integers number1 and number2 where:

      • number1 is in the range [1, 2, ..., 9]
      • number2 is in the range [1, 2, ..., 9]
      • Prints out the question:

          What is number1 - number2 ?
        

        where number1 ≥ number2       

  • After the first-grader enters the answer, the program displays the message indicating whether the answer was correct or not.

Using random numbers to make subtraction practice questions for 1st graders

Start with a plan containing the steps of the program:

import java.util.Scanner;

public class MyProg 
{
   public static void main(String[] args) 
   {
      Generate integer number number1 between 1-9

      Generate integer number number2 between 1-9

      Make sure number1 >= number2    // Do you remember how to do this ?  


      Print: What is number1number2 ?  // concatenation not covered yet...
      Read in answer

      if ( answer is correct )
         print "Correct"
      else
         print "Incorrect"
   }
}  

Develop program with the plan (translate the actions into Java statements)

Using random numbers to make subtraction practice questions for 1st graders

Write Java statement that perform each step of the plan:

import java.util.Scanner;

public class MyProg 
{
   public static void main(String[] args) 
   {
      int number1, number2;

      number1 = (int) (9*Math.random() + 1);
      number2 = (int) (9*Math.random() + 1);

      if ( number1 < number2 )
      {
         int helper;
         helper = number1;
	 number1 = number2;
	 number2 = helper;
      }

      System.out.print("What is ");
      System.out.print(number1);
      System.out.print("-");
      System.out.print(number2);
      System.out.println("?");

      Scanner input = new Scanner(System.in);
      int answer = input.nextInt();

      if ( answer == (number1-number2) )
         System.out.println("Correct");
      else
         System.out.println("Incorrect");
   }
}  

DEMO: demo/03-selections/05-random/Random4.java