Solved

Generating random numbers in VB 2005

Posted on 2007-04-04
3
292 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.Net How to Exit Sub - Exit Form??? 5 59
Extracting last characters in a substring 5 28
VB.Net Report Printing Issue 3 35
get column names from table in vb.net 8 30
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

821 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