Please make sure you have an understand in select case before moving on, otherwise you will be typing rubbish that you don't understand. The first new topic we will learn in this tutorial is the command Randomize. Obviously this command has one purpose, to make things random. Use Randomize to initialize VB's random number generator. This is called seeding. To avoid generating the same sequence of psuedo-random values, call Randomize before you call Rnd; but this only has to be done once. A good place to seed the psuedo-random number generator is in the Form's Load event:
Private Sub Form_Load() Randomize End Sub
Alright, for this tutorial, make a program with one text box and one command button. Name the command button cmdRandom and add a caption that says Randomize!. Now, for the text box, name it txtText and set the text property to nothing.
Double click the command button. The purpose of this program is going to be to generate a random number between 1 and 6, simulating the roll of a dice. The first thing we need to do is to write the statement for which the Select Case will be checking. This is done with a variable, usually a simple, one letter, undeclared variable. But in this variable, we are going to tell the program to generate a random number between 0 and 5. The reason we are starting at 0 is because all Programming languages start counting at 0.
x = Int(Rnd * 6)
That basically is saying that x is equal to a random number times 6, the random number being what the select case is going to output. Now we open our select case with the following:
Select Case x
We need to call 5 case statements, each with a different number and each setting the text of txtText to a new number. We start with this
Case 0 txtText="1"
All you do is repeat that 5 more times, chaging the two numbers to the next real number in line (0, 1, 2, 3,4..) The last thing to do, as with any program you have opened up a function, is close it. Close the Select Case with End Select
All in all, you should have a procedure that looks like the following:
x = Int(Rnd * 6) Select Case x Case 0 txtText = "1" Case 1 txtText = "2" Case 2 txtText = "3" Case 3 txtText = "4" Case 4 txtText = "5" Case 5 txtText = "6" End Select
Although simple, this tutorial was only written to touch on the Randomize and RND functions in Visual Basic, to help shed some light on them and further your understand about the concepts, not to write a simple program.