Solved

Generating random numbers in VB 2005

Posted on 2007-04-04
3
297 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 86

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 Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

623 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