Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

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

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
ljhodgett
Asked:
ljhodgett
1 Solution
 
Wayne Taylor (webtubbs)Commented:
Hi ljhodgett,

Use something like this....

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

Regards,

Wayne
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
Try this:

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

jpaulino
0
 
administradoresCommented:
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
iboutchkineCommented:
Dim generator As New Random
        Dim randomValue As Integer

        ' Generates numbers between 1 and 500, inclusive.
        randomValue = generator.Next(1, 501)
        MsgBox(randomValue)
0
 
AutoeformsCommented:
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
 
iboutchkineCommented:
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
 
Jorge PaulinoIT Pro/DeveloperCommented:
Any luck Lee ???
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now