?
Solved

vb.net - get random integer

Posted on 2009-12-16
8
Medium Priority
?
502 Views
Last Modified: 2012-05-08
hello there,
I would like to get a random integer between two different numbers from textbox..
how can I do that?
Function GetAge(TextA As TextBox, TextB As TextBox)
    Dim lb1 As Long, ub2 As Long
    If IsNumeric(TextA.Text) And IsNumeric(TextB.Text) Then
        lb1 = CLng(TextA.Text)
        ub2 = CLng(TextB.Text)
        GetAge = Int((ub2 - lb1 + 1) * Rnd + lb1)
    End If
End Function

Open in new window

0
Comment
Question by:XK8ER
8 Comments
 
LVL 23

Expert Comment

by:Kamaraj Subramanian
ID: 26069288
here it is

pass two numbers to the below function and get the random number
Public Function RandomNumber(ByVal MaxNumber As Integer, _
    Optional ByVal MinNumber As Integer = 0) As Integer
      
      'initialize random number generator
        Dim r As New Random(System.DateTime.Now.Millisecond)
  
      'if passed incorrect arguments, swap them
        'can also throw exception or return 0

        If MinNumber > MaxNumber Then
            Dim t As Integer = MinNumber
            MinNumber = MaxNumber
            MaxNumber = t
        End If

        Return r.Next(MinNumber, MaxNumber)

    End Function

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 26069290

GetAge = New Random(Date.Now.Millisecond).Next(lb1, ub2)

Open in new window

0
 
LVL 9

Accepted Solution

by:
Trideep Patel earned 200 total points
ID: 26069333
try this
from this link
http://www.mredkj.com/vbnet/randomnumbers.html



Public Function RandomNumber(ByVal low As Int32, ByVal high As Int32) As Integer
    Static RandomNumGen As New System.Random
    Return RandomNumGen.Next(low, high + 1)
  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!

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 26069375
trideep has posted the most correct code.  You need the "+1" on the second parameter so that the max value is actually included in the possible return values.

See Random.Next():
http://msdn.microsoft.com/en-us/library/2dx6wyd4.aspx

    "Return Value - 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."

In place of IsNumeric() you can use Integer.TryParse().
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 26069429
That is, of course, assuming the author meant inclusive "between" ;)
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 26069457
Pretty sure he does...his existing VB6 code of:

    GetAge = Int((ub2 - lb1 + 1) * Rnd + lb1)

Follows exactly the well documented formula :
http://msdn.microsoft.com/en-us/library/aa242020(VS.60).aspx

    "To produce random integers in a given range, use this formula:

            Int((upperbound - lowerbound + 1) * Rnd + lowerbound)

    Here, upperbound is the highest number in the range, and lowerbound is the lowest number in the range."
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 26069504
I was ribbing you. Good to see you online :)
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 26069517
Lol...doh!  I'm such an easy target...  =)
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

839 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