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
7
175 Views
Last Modified: 2013-11-26
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
Comment
Question by:ljhodgett
7 Comments
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 500 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

by:jpaulino
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

by:administradores
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 28

Expert Comment

by:iboutchkine
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

by:Autoeforms
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

by:iboutchkine
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

by:jpaulino
ID: 20280738
Any luck Lee ???
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now