1) Demonstrate how to generate a single random number...
2) Show you how to generate a set of random numbers...
... between 2 given values.
[color green]You MUST initialize the Random Number Generator BEFORE selecting the random numbers. In your Form_Load() event, simply put the word Randomize, thus:[/color]
[tt]
Private Sub Form_Load()
Randomize
End Sub
[/tt]
Generate a single random number between 1 and x
[tt]Dim MyValue as Integer
MyValue = Int((10 * Rnd) + 1) [color green]' Generate random value between 1 and 10.[/color][/tt]
Generate a random number between x and y
[tt]Dim MyValue as Integer
intNumber = Int((100 * Rnd) + 150) [color green]' Generate a random number between 150 (x) and 250 [/color]
[/tt]
{Set x (the 150 above) as the lowest number. Set y (the 100 above) to the highest number - x.)
Generate x random numbers between 1 and x, store in an array
[tt]Dim MyValue(9) as Integer
Dim I as Integer
For I = 0 To 9
MyValue(I) = Int((100 * Rnd) + 1)
Next[/tt]
This will generate 10 random numbers, store them in an array (MyValue), with the numbers between 1 and 100.
Generate x random numbers between y and z, store in an array
[tt]
Dim MyValue(5) as Integer
Dim I as Integer
For I = 0 To 5
MyValue(I) = Int((70 * Rnd) + 30)
Next[/tt]
(This will generate 6 random numbers between 30 and 70, then store them in the MyValue array.)
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.