Solved

Generating random numbers in VB 2005

Posted on 2007-04-04
3
287 Views
Last Modified: 2010-04-23
In VB6, I used to generate random numbers using the following code:

Dim nRandomNum As Integer

Math.Randomize
nRandomNum = Math.Rnd

I see in VB 2005 there is now a RandomNumberGenerator class. Should I be using this rather? And if so, can someone show me a simple example of how to use it?

Thanks
0
Comment
Question by:SETP
3 Comments
 
LVL 7

Accepted Solution

by:
dctuck earned 200 total points
ID: 18849401
Dim generator As New Random
Dim randomValue As Integer
randomValue = generator.Next(minvalue, maxvalue)
0
 
LVL 18

Assisted Solution

by:Ajay Sharma
Ajay Sharma earned 150 total points
ID: 18849595
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 150 total points
ID: 18851791
One big "gotcha" when using the Random class is the second parameter to the Next() method is NOT included in the return set.

See:
http://msdn2.microsoft.com/en-us/library/2dx6wyd4.aspx

    "Return Value --> A 32-bit signed integer greater than or equal to minValue and less than maxValue; that is, the range of return values includes minValue but not maxValue."

This is OFTEN missed by new developers!

So if you want values between 1 and 5 INCLUSIVE then you need:

    randomValue = generator.Next(1, 6) ' returns 1 to 5

and NOT:

    randomValue = generator.Next(1, 5) ' returns 1 to 4
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now