Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • 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
 
jpaulinoCommented:
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
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!

 
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
 
jpaulinoCommented:
Any luck Lee ???
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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