1. Home
  2. Computing & Technology
  3. Visual Basic
Extending the Lucky Seven Code
More Uses For Lucky Seven Code
 Join the Discussion
Log into the About VB Forum Here!
 
 Elsewhere on the Web
• Advanced!
How Visual Basic Generates Pseudo-Random Numbers

 

Finished with Chapter 2? Good! Let's consider the Music Trivia program from Chapter 1 in light of what we have learned in Chapter 2. You probably realize that the Music Trivia program is ... well ... a pretty 'trivial' program. I mean, what is a trivia contest with exactly one question? But, using the techniques from Chapter 2, you can extend this program and create a REAL trivia contest. Let's see how.

The Text properties of the Label objects in the Lucky Seven program are calculated using a random number function as follows:

Label1.Text = CStr(Int(Rnd() * 10))

The value 10 is used because there are 10 possible digits to choose from. If we were using the ancient Mayan number system which is based on 20 digits, we would code this as:

IndexValue = CStr(Int(Rnd() * 20))

...and then we would have to convert our "index value" into one of the Mayan number glyphs. So, in general, we can select one of "N" objects randomly using this general formula. The actual formula for selecting a random number in a range from HighestValue to LowestValue looks like this:

Int((HighestValue - LowestValue) * Rnd) + LowestValue

Note that this formula will select LowestValue but not HighestValue. To include HighestValue in the selection, add 1. Suppose, then, that we had 8,743 questions in our Trivia Contest. We could randomly select one of the questions the same way.

IndexValue = CStr(Int(Rnd() * 8743))

This leaves a whole bunch of questions still to be answered, however. For one, where are these questions stored for the program? And another is, how do we display a different picture for different answers?

Chapter 3 - discussed in the lesson to be delivered next time - includes a program called the "Online Shopper". We'll talk more about it then!

Back to the first page >>>   Beyond the Book: Source Code Control > Page 1, 2, 3
Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic

©2009 About.com, a part of The New York Times Company.

All rights reserved.