Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1

Chapter 10 Creating Modules and Procedures 261


and this value is assigned to the Text property of the lblRate label on the form after
each spin. Now remove the Randomize function from the Form1_Load event procedure,
so that while you test the project, your results will follow a familiar pattern.


  1. Scroll down in the Code Editor to the Form1_Load event procedure, and remove or
    “comment out” (place a comment character (‘) before) the Randomize function.
    Now, each time that you run this program, the random numbers generated will follow
    a predictable pattern. This helps you test your code, but when you’re finished testing,
    you’ll want to add the function back again so that your results are truly random.
    Now you’ll run the program.


Run the Track Wins program


  1. Click the Start Debugging button to run the modified Track Wins program.

  2. Click the Spin button 10 times.


The first five times you click Spin, the win rate stays at 100 .0%. You’re hitting the
jackpot every time. As you continue to click, however, the win rate adjusts to 83 .3%,
71 .4%, 75 .0% (another win), 66 .7%, and 60 .0% (a total of 6 for 10). After 10 spins, your
screen looks like this:

If you continue to spin, you’ll notice that the win rate drops to about 28%. The HitRate
function shows that you were really pretty lucky when you started spinning, but after
a while reality sets in.


  1. When you’re finished with the program, click the End button.


The program stops, and the development environment returns. You can add the
Randomize function to the Form1_Load event procedure again to see how the program
works with “true” randomness. After about 100 spins (enough iterations for statistical
variation to even out a little), you should be close to the 28% win rate each time that
you run the program. If you like numbers, it is an interesting experiment.


  1. Click the Save All button on the Standard toolbar to save your changes.

Free download pdf