# how to generate 10 digit random numbers

### Question

• Hi any body help, how to generate 10 digit random number using vb.net ( it like phone numbers)

Monday, September 15, 2008 12:56 PM

• I'm not done with this horse yet, either.  Here's an override for the Random class.  It perserves all of the functionality of "System.Random", but makes the "Next" method work with Longs instead of Integers.

You would call is using:

Dim r As New Random

r.Next(1000000000, 10000000000)

Code Snippet

Public Class Random

Inherits System.Random

Public Shadows Function [Next]() As Long

Return CType(Int(MyBase.NextDouble * Long.MaxValue), Long)

End Function

Public Shadows Function [Next](ByVal maxValue As Long) As Long

Return CType(Int(MyBase.NextDouble * maxValue), Long)

End Function

Public Shadows Function [Next](ByVal minValue As Long, _

ByVal maxValue As Long) As Long

Select Case True

Case minValue < maxValue

Return CType(Int(MyBase.NextDouble * (maxValue - minValue)), _

Long) + minValue

Case minValue = maxValue

Return minValue

Case Else

Throw New ArgumentOutOfRangeException

End Select

End Function

End Class

Tuesday, September 16, 2008 12:50 AM

### All replies

• You could generate 10 random numbers from 0 to 9 and string them together.

Dim Number As New System.Text.StringBuilder

Dim Rnd As New Random

For count As Integer = 0 To 9

Number.Append(Rnd.Next(0, 10).ToString)

Next

Dim Result As Decimal = CDec(Number.ToString)

Monday, September 15, 2008 1:13 PM
• Here's a way to do it in one pass . . .

Code Snippet

Private Function GetRandom10() As Long

Static r As New Random

Return CType(r.NextDouble * 9000000000 + 1000000000, Long)

End Function

Monday, September 15, 2008 6:09 PM
•  Dave299 wrote:
 You could generate 10 random numbers from 0 to 9 and string them together.   Dim Number As New System.Text.StringBuilder Dim Rnd As New Random For count As Integer = 0 To 9 Number.Append(Rnd.Next(0, 10).ToString) Next Dim Result As Decimal = CDec(Number.ToString)

Just to add to Dave's good example:

Dim Number As New System.Text.StringBuilder

Dim Rnd As New Random

For count As Integer = 0 To 10

Number.Append(Rnd.Next(0, 10).ToString)

Next

Dim Result As String = Number.ToString("###-###-####")

Monday, September 15, 2008 8:43 PM
• I'm not done with this horse yet, either.  Here's an override for the Random class.  It perserves all of the functionality of "System.Random", but makes the "Next" method work with Longs instead of Integers.

You would call is using:

Dim r As New Random

r.Next(1000000000, 10000000000)

Code Snippet

Public Class Random

Inherits System.Random

Public Shadows Function [Next]() As Long

Return CType(Int(MyBase.NextDouble * Long.MaxValue), Long)

End Function

Public Shadows Function [Next](ByVal maxValue As Long) As Long

Return CType(Int(MyBase.NextDouble * maxValue), Long)

End Function

Public Shadows Function [Next](ByVal minValue As Long, _

ByVal maxValue As Long) As Long

Select Case True

Case minValue < maxValue

Return CType(Int(MyBase.NextDouble * (maxValue - minValue)), _

Long) + minValue

Case minValue = maxValue

Return minValue

Case Else

Throw New ArgumentOutOfRangeException

End Select

End Function

End Class

Tuesday, September 16, 2008 12:50 AM
• Steve,

That's sweet, good job man!

Tuesday, September 16, 2008 2:04 AM
• Thanks

Thursday, September 18, 2008 10:35 AM
• "Private Function GetRandom10() As Long

Static r As New Random

Return CType(r.NextDouble * 9000000000 + 1000000000, Long)

End Function"

What am I missing?  Why double?

Private Function GetRandom10() As Integer

Static r As New Random

Return 1000000000 + r.Next(1000000000)

End Function

Are random doubles computed with a different algorithm than random integers?

Thursday, September 18, 2008 1:56 PM
•  JohnWein wrote:
 "Private Function GetRandom10() As Long       Static r As New Random       Return CType(r.NextDouble * 9000000000 + 1000000000, Long) End Function" What am I missing?  Why double? Private Function GetRandom10() As Integer Static r As New Random Return 1000000000 + r.Next(1000000000) End Function Are random doubles computed with a different algorithm than random integers?

Correct me if I'm wrong, but with that function, you will never get a result larger than 1999999999.  How would it ever produce a value like 9999999999   (the largest possible 10 digit number)?  Since the largest integer (unsigned) is 2147483647, the built in Random class could never produce a number this large.  I think that's why double was chosen.

Chris

Thursday, September 18, 2008 2:30 PM
• Chris:

Yep.  I was definitely missing something.  I was adding 1 to 9 digits to get 10 digits, but a phone number is 1 + 10  digits.

I'm still not sure that random doubles cover a wider range than random integers.  Without knowing for sure, I'd use Dave's 10 individual digits.

Thursday, September 18, 2008 3:32 PM
• Since the OP mentioned wanting random phone numbers and since I have been trying to study LINQ and related new technologies, here is my entry that returns a string:

Code Snippet

Private Shared Function GetRandom10() As String

Static rnd As New Random()

Static digits As String = "0123456789"

Return digits.Select(Function(c) digits.Chars(rnd.Next(0, 10))).ToArray()

End Function

"When all you have is a hammer, everything looks like a nail"

Chris

Thursday, September 18, 2008 3:40 PM