back
Visual Basic Random Number Generator Cont.
Adapted from An Introduction to Programming in Visual Basic by Beth Brown and Bruce Presley

Random numbers are used all the time in programming. Screen savers, for example, use random numbers to display graphics. Video games depend on random numbers to generate a huge amount of the programming you see when you play.

The last random number exercise generated random numbers between 0 and 1. Now we will expand that so that you can control the range of random numbers the user will see.

First, take a look at how I've modified the form:



First, modify your form to include buttons that go from 0 to 10 all the way to 40 to 50 (5 buttons).

The code will need to change slightly. Whereas before you typed:

lblRandNum1.Caption = Rnd

Now, we will change it in a couple ways. If you wish to generate a random number between 0 and a certain number, say 10, all you need to do is this:

lblRandNum1.Caption = Rnd * 10

If you wish to generate a random number between two numbers, say between 10 and 40, you need to apply a simple formula. The formula you need is:



Which would translate to:



Finally, if you want to round the numbers off to whole numbers, you can add the int function (short for integer):

Assignment

Modify your random number project to include buttons for each of the following:

0-10
10-20
20-30
30-40
40-50

Make the button that displays random numbers from 40-50 display whole numbers.