Generating Random Numbers….
cmdrange cmdprize txtdisplay
Setup
the following form with the appropriate labels and textboxes. Name them as displayed
Understanding
the Code. There are three mini programs
on the form separated by lines. Lets look at the first program
Here
is the code for the button
Private
Sub cmdrandom_Click()
'will
generate a random number between 1 and 10
The computer will pick 10 random numbers starting
with 1 as the lowest value
Dim x
As Integer 'holds a random number
x =
Int(Rnd(1) * 10 + 1) ‘generates the
random number
’*10 represents
how many numbers in range
‘+1
represents the lowest value
txtdisplay.Text
= x ‘displays number in text box
Here
is the button code
This time I allow the user to decide what the range
of the random numbers will be.
Private
Sub cmdrange_Click()
'will
generate a random number between two entered values
Dim
low As Integer
Dim
high As Integer
Dim
number As Integer
low
= txtlow.Text ‘gets user entered value
high
= txthigh.Text
range
= high - low + 1 'finds the difference
and adds 1
number
= Int(Rnd(1) * range + low) 'generates
the random number using the variables
lblnumber.Caption
= number
Don’t
get frustrated now
Private Sub cmdPrize_MouseMove(Button As Integer, Shift As
Integer, x As Single, y As Single)
'will
cause button to move to a random location when cursor is over button
Dim
xloc As Integer 'xlocation of button
Random
Numbers can be fun J
Dim
yloc As Integer 'ylocation of button
xloc = Int(Rnd(1) * 4000 + 1) ‘generates a number between
1 and 4000 for x coordinate
yloc
= Int(Rnd(1) * 4000 + 1)’ generates a number between 1 and 4000 for y
coordinate
cmdPrize.Left
= xloc ‘moves button to
xloc
cmdPrize.Top
= yloc ‘moves button to
yloc
So
every time you move the mouse over the button , the button moves somewhere else
and you never get to click on it……J