Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 938437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:38:39+00:00 2026-05-15T21:38:39+00:00

I want to generate random number, which is 9 digits including leading zero if

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:38:39+00:00Added an answer on May 15, 2026 at 9:38 pm

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Java: generating random number in a range I want to generate a
I'm trying to create an android application which will generate random series of values
I want to generate random numbers manually. I know that every language have the
Say you want to generate a matched list of identifiers and strings enum {
I'm not so sure how to write this: I want to generate a random
I have a very basic random number function which generates a random number from
i want to generate a sequence of unique random numbers in the range of
So here is the deal: I want to (for example) generate 4 pseudo-random numbers,
I'm using C# and I need to generate a random 10 digit number. So
I am generating a random number of 32 bytes (which from Microchip PIC24F). When

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.