INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

How-to

How to generate Random Numbers.. Ver 2 by NoCoolHandle
Posted: 4 May 05

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

Back to Visual Basic (Microsoft) VB.NET FAQ Index
Back to Visual Basic (Microsoft) VB.NET Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close