Overview Schedule Announcements Resources Assignments Home

CSC 121: Computer Science I, Spring 2005

Lab 4

Laboratory 4 - Random Numbers and Project 1

Goals:
To experiment with randomization in a simulation. To complete several parts of project 1.
Getting started:
The Steps of the Lab:
  1. Open the while project
    Read Open the editor for the While class and read the genTen method. It displays 10 pairs of random numbers in a terminal window: a double random number and an int random number derived from the double. Create an instance of the while class and invoke the genTen() method. Observe the results and ask questions if you do not understand before going to the next step.
    Add Add the following methods to While class:
    1. Write a method, coinFlip, that uses a while loop to "flip" a coin 6 times. In the while loop generate a random number (int) in the range 0, 1. If the number generated is odd, display "heads" in the terminal window, otherwise display "tails". Invoke the method several times and keep a (mental) count of the number of times heads and tails occur each time. Although the counts you observe vary each time the method is run, what total result would you expect to see after invoking the method hundreds of times?
    2. Write a method, willItQuit(), that uses a while loop to generate and display random numbers in the range 1 to 100 until the number generated is greater than 50. Run your method several times and observe the results. Assuming your loop to have the correct logic, what would you conclude about the random number generator if the while loop never terminated on a particular run?
    3. Write a method, windowPos(), that uses a while loop to display five random (xPosition, yPosition) pairs in the ranges 0...myCanvas.getWidth() and 0...myCanvas.getHeight(), respectively.
    Complete Work with your partner to complete the following parts of Project 1: the first three bullets and the fifth bullet.
Overview Schedule Announcements Resources Assignments Home

Valid HTML 4.01!Valid CSS!DePauw University, Computer Science Department, Spring 2005
Maintained by Brian Howard (bhoward@depauw.edu). Last updated