[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
Solved

# I am trying to find out how to make a random number between 1 - 500 in vb.net 2005

Posted on 2007-11-14
Medium Priority
205 Views
Hi,

I am trying to find a function that will produce a random number between 1 - 500 when a button is pressed then send it to a msgbox please.

Many Thanks
Lee
0
Question by:ljhodgett
[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

LVL 48

Accepted Solution

Wayne Taylor (webtubbs) earned 2000 total points
ID: 20279430
Hi ljhodgett,

Use something like this....

Dim r As New Random
MsgBox(Convert.ToInt32(r.NextDouble * 500))

Regards,

Wayne
0

LVL 48

Expert Comment

ID: 20279434
Try this:

Dim rnd As New Random
MsgBox("This is an random number:" & rnd.Next(1, 500))

jpaulino
0

LVL 5

Expert Comment

ID: 20279437
I give you a function below

Usage: GetRandomNumber(1, 500)

Private Function GetRandomNumber(ByVal vnMinimumNumber As Integer, ByVal vnMaximumNumber As Integer)
Randomize()
GetRandomNumber = CInt(Int((vnMaximumNumber - vnMinimumNumber + 1) * Rnd() + vnMinimumNumber))
End Function

0

LVL 28

Expert Comment

ID: 20279445
Dim generator As New Random
Dim randomValue As Integer

' Generates numbers between 1 and 500, inclusive.
randomValue = generator.Next(1, 501)
MsgBox(randomValue)
0

LVL 8

Expert Comment

ID: 20279956
iboutchkine is correct in the best way to do this. however if you always generate a new generator (ie, generator As New Random) your number will not be truly random. It is better to create the random class that will persist for the entire time you need random numbers.

greg

0

LVL 28

Expert Comment

ID: 20280068
you define a new Random on the top of the form and in the event where you generate you number you will get the random number
0

LVL 48

Expert Comment

ID: 20280738
Any luck Lee ???
0

## Featured Post

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
###### Suggested Courses
Course of the Month15 days, left to enroll

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

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