How do I generate a 10 digit random number in Visual Basic 2005

Posted on 2007-10-17
Last Modified: 2013-11-27
How can I generate a random 10 digit number in Visual Basic 2005?

Thank you
Question by:OsirisJa
    LVL 48

    Accepted Solution

    Try this

    Dim num As New Random
    Dim result As Long
    For x As Byte = 0 To 9
      result &= num.Next(1,10)
    Next x

    MsgBox result

    LVL 85

    Expert Comment

    by:Mike Tomlinson
    If you want the number 0 (zero) to be included then:

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim num As Long = Long.Parse(RandomNumber(10))
        End Sub

        Private Function RandomNumber(ByVal length As Integer) As String
            Static R As New Random
            Dim num As String
            num = R.Next(1, 10) ' 1 to 9 for FIRST digit
            For i As Integer = 2 To length
                num &= R.Next(0, 10) ' 0 to 9 for subsequent digits
            Return num
        End Function

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…

    746 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

    18 Experts available now in Live!

    Get 1:1 Help Now