Tuesday, December 9, 2008

Wed December 10: Actual for real randomness and pretty colors too!

Greetings VB coders!!!



Today we will make a project that does several things:



A) Creates truly random numbers that are DIFFERENT everytime you start the program



B) Sets a limit to which number values are used -- basically, the smallest and the largest used



C) Uses these random numbers to populate an empty array with values from another array




D) Uses a for loop (which we still seem to have some issues with, at least some of us)




Here's how we do it:



1) Start off by making a project called LottsaRandom, with your name appended to the end of it




2) Add two buttons and one TextBox to this project. Rename the first button to "Mustard", & change its text to Mustard as well. Rename the second button to "Ketchup", and change its text to read ketchup




3) rename the textbox to "codeSheet" and change its text to read "secret codes will go here"

4) get into the code for this project, and just below where it says "Public Class Form1" create the following:

a) an Integer variable called "whatChar"
b) a String array that has all 26 letters of the alphabet, AND the numerals 0 to 9 AND the characters ! @ # $

please not that each one of these items is an element in your String array. If you've forgotten how to make a String array, you have one as an example in our Ferris Bueller project

c) create an empty String array like this ----> Dim codes(7) As String
notice that this is also a String array, but it has spaces for elements, not actual elements. Think of an empty egg carton.


pause, take a breath, now for the real fun:

OK, so now we have to make a SUB procedure (remember those? it seems like just yesterday...)
This SUB has to be able to create truly RANDOM numbers, and it cannot start with the same random set of numbers every time!

PLUS, the random values must fall within a certain range. Why? Well, because we are trying to randomly pick out certain elements from the array that has stuff in it, and put them in the array that doesn't have anything yet. There are only 40 elements in the array that's filled, so we don't need to get random values going up to 100, or 100; that would make no sense

Here's how we do it:

Public Sub SevenRandomNumbers( )
Dim A As New Random 'set a random seed using the RANDOM class
Dim x As Integer 'set an integer to use in the loop
For x = 0 To 6 'go thru loop to fill codes array
whatChar = A.Next(0, 40) 'number of elements in the Letters array
codes(x) = whatChar 'whatever x value is, that element in the empty array is filled
Next x
End Sub


OK, so now how do we "call" that SUB procedure? Simple, we get into the code for the "mustard" button (mustard, seed, get it? Oh well.....) and we call that SUB. Then we use the results we get to fill the textbox, like so:

SevenRandomNumbers( ) 'call the sub
Dim x As Integer
For x = 0 To 6
codeSheet.Text = codeSheet.Text & codes(x) & " "
Next x

OK, that oughtta do it. Lets try and run that program, and see what we get!

Cheers,
Mr. L

No comments: