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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to generate Random Numbers.. Ver 2

How-to

How to generate Random Numbers.. Ver 2

by  NoCoolHandle  Posted    (Edited  )
There is already a great resource here on the rnd function.

I will not cover this, what I will look at is the RANDOM class.

One issue I have bumped into before needing a random integer value that is between 2 values (a high and a low).

Using the rnd function usually requires that you convert part of the double to be your Whole number and then see if it is larger or smaller than your min or max values, and if it isn't calling it again until you hit a valid return.

I.E. I have 275 images and want to randomly get 1 of the images. Picking a 3 digit section of the returned double will have about a 60% chance of being too great!

This is where the RANDOM class comes in!
Code:
Dim r as Random
msgbox r.Next(1,275)
will return a whole number(integer) between 1 and 275. No testing needed.

2 Lines of code and exactly what the Dr. ordered.

Happy coding!

Rob
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