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

  • SEARCH
  • Home
  • 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 8871835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:05:15+00:00 2026-06-14T18:05:15+00:00

In Python, how to generate a 12-digit random number? Is there any function where

  • 0

In Python, how to generate a 12-digit random number? Is there any function where we can specify a range like random.range(12)?

import random
random.randint()

The output should be a string with 12 digits in the range 0-9 (leading zeros allowed).

  • 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-06-14T18:05:16+00:00Added an answer on June 14, 2026 at 6:05 pm

    Whats wrong with a straightforward approach?

    >>> import random
    >>> random.randint(100000000000,999999999999)
    544234865004L
    

    And if you want it with leading zeros, you need a string.

    >>> "%0.12d" % random.randint(0,999999999999)
    '023432326286'
    

    Edit:

    My own solution to this problem would be something like this:

    import random
    
    def rand_x_digit_num(x, leading_zeroes=True):
        """Return an X digit number, leading_zeroes returns a string, otherwise int"""
        if not leading_zeroes:
            # wrap with str() for uniform results
            return random.randint(10**(x-1), 10**x-1)  
        else:
            if x > 6000:
                return ''.join([str(random.randint(0, 9)) for i in xrange(x)])
            else:
                return '{0:0{x}d}'.format(random.randint(0, 10**x-1), x=x)
    

    Testing Results:

    >>> rand_x_digit_num(5)
    '97225'
    >>> rand_x_digit_num(5, False)
    15470
    >>> rand_x_digit_num(10)
    '8273890244'
    >>> rand_x_digit_num(10)
    '0019234207'
    >>> rand_x_digit_num(10, False)
    9140630927L
    

    Timing methods for speed:

    def timer(x):
            s1 = datetime.now()
            a = ''.join([str(random.randint(0, 9)) for i in xrange(x)])
            e1 = datetime.now()
            s2 = datetime.now()
            b = str("%0." + str(x) + "d") % random.randint(0, 10**x-1)
            e2 = datetime.now()
            print "a took %s, b took %s" % (e1-s1, e2-s2)
    

    Speed test results:

    >>> timer(1000)
    a took 0:00:00.002000, b took 0:00:00
    >>> timer(10000)
    a took 0:00:00.021000, b took 0:00:00.064000
    >>> timer(100000)
    a took 0:00:00.409000, b took 0:00:04.643000
    >>> timer(6000)
    a took 0:00:00.013000, b took 0:00:00.012000
    >>> timer(2000)
    a took 0:00:00.004000, b took 0:00:00.001000
    

    What it tells us:

    For any digit under around 6000 characters in length my method is faster – sometimes MUCH faster, but for larger numbers the method suggested by arshajii looks better.

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

Sidebar

Related Questions

In Python, what is the best way to generate some random number using a
Is it possible to generate the same random number in python as in java
Are there any tools which generate a project layout for python specific projects, much
Can python generate path with multi-sub-paths by short form syntax like this ? vars=project/{DEBIAN,usr/{bin,usr/{applications,icons,share}},computer}
How do we use python to generate a four digit counter? range(0,9999) will have
Is there a disadvantage to using a dynamic Python file to generate the CSS
I need to generate a string with n characters in Python. Is there a
I would like to generate Python Expect (pexpect) code automatically, does something like autoexpect
I need to generate random text strings of a particular format. Would like some
Is there a built-in method / module in Python to generate letters such as

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.