Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random numbers

Numbers

Random numbers

by  JESTAR  Posted    (Edited  )
This FAQ will:

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 (y)[/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.)




Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top