Generate Random Number Within a predetermined range - VB.NET

Posted on 2005-03-04
Medium Priority
Last Modified: 2008-01-09
I am trying to generate a random number in a VB .Net application but I need the number to be within a predetermined range (the range will be 0 to TotalCount).

I know this can be done, but it's been forever since I've done it and I nolonger have MSDN information files available.

If anyone has any ideas, let me know.


Question by:Rorc
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

Accepted Solution

inq123 earned 2000 total points
ID: 13460247
Hi Rorc,

Please see this page: http://www.brettb.com/RandomNumbersInDotNet.asp

Basically something like:

Dim RandomClass As New Random()
Dim RandomNumber As Integer
RandomNumber = RandomClass.Next(TotalCount)

Should do it


Author Comment

ID: 13460533
Thank you, I knew it could be done but just couldn't remember how.
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13460557
Just remember that the value passed into the Next method is NOT included.  In the example below, a number between 0 and 14 inclusive will be returned:

        Dim TotalCount As Integer = 15
        Dim RandomClass As New Random
        Dim RandomNumber As Integer
        RandomNumber = RandomClass.Next(TotalCount)

Here is the Random.Next() help:

From the Return value remarks:
"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. If minValue equals maxValue, minValue is returned."

This also holds true for the Next() method that acceps two parameters.  The first value will be included but the second will not:

        Dim RandomClass As New Random
        Dim RandomNumber As Integer
        RandomNumber = RandomClass.Next(4, 15)

The above will return a value between 4 and 14 inclusive.



Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Introduction to Processes
Suggested Courses

770 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