tags:

views:

81

answers:

3

Hi,

I want to generate random number, which is 9 digits including leading zero if the number is less than 9 digits, say 123 will be 000000123. I have the following code which doesn't include leading zero :

Dim RandomClass As New Random()
Dim RandomNumber = RandomClass.Next(1, 999999999)

Thanks.

+2  A: 

EDIT: While I still quite like my "individual digits" approach below, there's an easier way - just give a custom number format:

C#:

Random rng = new Random();
int number = rng.Next(1, 1000000000);
string digits = number.ToString("000000000");
Console.WriteLine(digits);

VB:

Dim rng As New Random
Dim number As Integer = rng.Next(1, 1000000000)
Dim digits As String = number.ToString("000000000") 
Console.WriteLine(digits)

EDIT: As has been pointed out in the comments, a format string of D9 will also do the job:

Dim digits As String = number.ToString("D9") 

Personally I'd have to look up exactly what that would do, whereas I'm comfortable with custom number formats - but that says more about me than about the code :)


Rather than generating a single number between 1 and 999999999, I would just generate 9 numbers between 0 and 9. Basically you're generating a string rather than a number (as numerically 000000000 and 0 are equivalent, but you don't want the first).

So generate 9 characters '0' to '9' in a Character array, and then create a string from that.

Here's some sample C# code:

using System;

class Test
{
    static void Main(string[] args)
    {
        Random rng = new Random();
        string digits = GenerateDigits(rng, 9);
        Console.WriteLine(digits);
    }

    static string GenerateDigits(Random rng, int length)
    {
        char[] chars = new char[length];
        for (int i = 0; i < length; i++)
        {
            chars[i] = (char)(rng.Next(10) + '0');            
        }
        return new string(chars);
    }
}

... and converting it to VB:

Public Class Test

    Public Shared Sub Main()
        Dim rng As New Random
        Dim digits As String = Test.GenerateDigits(rng, 9)
        Console.WriteLine(digits)
    End Sub

    Private Shared Function GenerateDigits(ByVal rng As Random, _
                     ByVal length As Integer) As String
        Dim chArray As Char() = New Char(length  - 1) {}
        Dim i As Integer
        For i = 0 To length - 1
            chArray(i) = Convert.ToChar(rng.Next(10) + &H30)
        Next i
        Return New String(chArray)
    End Function

End Class

One point to note: this code can generate "000000000" whereas your original code had a minimum value of 1. What do you actually want the minimum to be?

Jon Skeet
Then minimum should be 1. It's starting from 1.
Narazana
You don't have to use a custom number format, there's a standard format code for padding zeros. `number.ToString("D9")`
Richard Szalay
And shouldn't the maximum number used in the call to Random be 1000000000 so that 999999999 might be included in the generated result?
Chris Dunaway
@Chris: Yup, good call :)
Jon Skeet
Dim rng As New RandomDim digits = rng.Next(1, 999999999).ToString("D9"):D
Angkor Wat
@Richard @Bayonian: I nearly got as far as looking that up. I suspected there was something there. I think the custom format makes the point more forcibly though. It depends on how happy you are with the standard formats. I'm not as happy with them as I should be. I'll edit the answer to mention this though.
Jon Skeet
@Jon While I agree that the custom format makes it more explicit, it also makes it more difficult on first glance to see how many zeros are there, making it more difficult to see if you've made a mistake :)
Richard Szalay
+3  A: 

You need to declare your variable as type string. Integer can't have leading zero. Check my sample below:

 Dim RandomClass As New Random()


    Dim randomString As String


    For i = 1 To 1000 Step 1

        randomString = RandomClass.Next(1, 999999999).ToString

        If randomString.Length < 9 Then
            randomString = randomString.PadLeft(9, "0")
        End If
        Response.Write(randomString & "<br/>")
    Next
Angkor Wat
This works quite well.
Narazana
This is what I would have done. I was surprised to see these other answers with char[]s and all that. Your way is baked into the framework.
aape
+1  A: 

Is 999999999 a legal value? If so...

'
Dim myRand As New Random
'
Private Function NineDigitRand() As String
    'return a numeric string between 000000001 and 999999999 inclusive
    Return myRand.Next(1, 1000000000).ToString("d9")
End Function
dbasnett