FIT 100

Lab 12:  Randomization and Procedures

Spring 2001

Reading to be done prior to Lab 12:

·         p. 272-275 (Random Numbers) in Chapter 10 of Computer Programming Fundamentals with Applications in Visual Basic 6.0

 

Introduction:

This lab adds to the Lab 11 practice with condition statements and procedure calls.  You will add simple code that incorporates the use of random numbers as you alter the voter’s ballot that tallies votes for two candidates.

 

Objectives:

  1. To use the Randomize statement and the Rnd function to generate a random number
  2. To use nested condition statements to check for true/false statements among multiple variables.

 

TO DO: 

 

  1. Open up the ChicagoVote project in Visual Basic. 

 

  1. Change the captions of your form, labels and radio buttons to match the form below:

 

  1. Save the project.
    (You may wish to save the project and the form under another name in order to have separate versions of the work done for Lab11 and Lab 12)

 

The Scenario:

This is the now the ballot for the presidential elections for Florida in 2004.  Your goal is to add to the program that tallies the number of votes for a candidate.  You will only be adding statements to your existing program, not taking any away.

You are now voting in Florida.  Many of you may remember the fiasco of the “hanging chad” vote count from the 2000 election.  Well, the problem has not been fixed, and in fact it has now moved into the electronic voting realm. 

 

Many people suspect that the election in Florida was fixed, but the truth can never really be known.  However, you are now responsible for the design and functionality of the electoral ballot for the 2004 election.  Secretly, you are being paid to alter the vote count slightly in favor of George Bush.  However, you have to be sly about it.  The Chicago Ballot from Lab 11 is way too obvious to use on the general public. 

 

You will now alter the ballot slightly to make subtle voting changes that benefit the Bush campaign.

 

In the readings for lecture and lab, you learned a little about using a particular code statement and function to create a randomly generated number.  You will use the concept of random numbers as a way choose how many votes a particular candidate will receive on the click of the vote button. 

 

The Randomize Statement

When a person clicks the Vote button, a random number will be generated and stored in a variable. 

 

 

The only place in the program you will use the random number function is in the Vote button Click event.  The rest of the code you write for this program will be located there.

  1. Inside the Vote button click event, declare a variable to hold the random number that is generated:

Dim <some variable name> as Integer

  1. Initialize the random number generator inside the Vote click event:

Randomize

  1. Generate a random number and store the value in the variable you declared at the top of the Vote click event. 

    You only need to check the random number to see if it is odd or even.  The easiest way to do that is to make sure that the numbers generated will be either 1 or 2.  Since you know that the variable declared to hold the values will only accept integers, then you need to make sure the random number generated is an integer as well:

 

<some variable name> = Int((Rnd * 2) + 1)

 

  1. With that done, you now move inside of the condition statements that check to see who is getting the vote.  Inside the condition statements you need to use nested If/Then statements to decide how many votes each candidate will receive on each click of the Vote button.

  2. Create a nested If/then/else statement:

·         This nested If/Then will be just under the statement
If theCan = "George" Then

·         It should check the value of the random number.  IF it is 1 (an odd number), then increment the votes for George by 1.

·         Otherwise, increment the votes for George by 2.

  1. Create a second nested If/then/else statement:

·         The second nested If/then will be just under the statement
ElseIf theCan = "Some slob" Then

·         It should also check the value of the random number.  IF it is 1 (an odd number), then increment the votes for the other candidate by 1.

·         Otherwise, DO NOT INCREMENT!

  1. Run your program.  Do the labels display a subtle, but inconsistent increase in the number of votes for the candidates?

  2. Go into your ClickCheck procedure and change the number of times someone can click the Vote button to 5.  (Just seeing if you can easily find and alter the code you created)

 

  1. Run your program again.  Are you now limited to 5 votes per session instead of 10?
     
  2. Save your project to disk or FTP it to your Dante account.

 

  1.  DONE!!!!!