Solved

Generating random numbers in VB 2005

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
XML error not understood 5 33
Visual Studio hangs on running project 6 86
VB.net Is there code behind PowerPivot 2 72
How to call new form in VB2013? 4 37
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

710 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