October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

Generate 10-Digit Random Numbers in Python

Learn the correct Python bounds for ten-digit random integers, how to preserve leading zeros, and when to use the secrets module.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To generate a positive integer with exactly ten digits, call random.randint(1_000_000_000, 9_999_999_999). If you need a ten-character value that may begin with zero, generate a value from zero and format it as a zero-padded string instead.

Generate a ten-digit integer

Python’s randint(a, b) includes both endpoints. The smallest positive ten-digit integer is 1,000,000,000, and the largest is 9,999,999,999, so this range excludes nine-digit results and avoids an eleven-digit upper bound. See the Python random module documentation.

import random

number = random.randint(1_000_000_000, 9_999_999_999)
print(number)

Alternatively, random.randrange(1_000_000_000, 10_000_000_000) works because randrange excludes its stop value. Do not use randint(10**9, 10**10): its inclusive upper endpoint, 10,000,000,000, has eleven digits.

Include leading zeros when the output needs ten characters

An integer does not retain leading zeros. If the requirement is a fixed-width code or identifier, produce a string with a width of ten:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import random

number = random.randint(0, 9_999_999_999)
digits = f"{number:010d}"
print(digits)

If the generated value is 42, digits is 0000000042. Use the string for display or storage when those zeros matter; converting it back to an integer removes them.

Use a cryptographic generator for security-sensitive values

Python’s random module uses a deterministic pseudorandom generator and its documentation warns that it should not be used for security purposes. For passwords, authentication, security tokens, or other values that must be difficult to predict, use secrets, which is intended for cryptographically strong random values. See the Python secrets module documentation.

Positive ten-digit integer

import secrets

secure_number = secrets.randbelow(9_000_000_000) + 1_000_000_000

randbelow(n) returns a value from zero up to, but not including, n; adding 1,000,000,000 therefore gives a positive ten-digit integer.

Ten characters with possible leading zeros

import secrets

secure_digits = f"{secrets.randbelow(10_000_000_000):010d}"

This creates a ten-character string, including any leading zeros, using the security-oriented generator.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Generate several values and handle duplicates

Repeated calls are appropriate when duplicate values are acceptable. For example, this creates five ordinary pseudorandom integers; the same value can appear more than once:

import random

numbers = [random.randint(1_000_000_000, 9_999_999_999) for _ in range(5)]

If every result must be unique, repeated independent draws are not enough. Track values already selected and draw again when a duplicate appears, or use sampling without replacement when selecting from a defined sequence. The random documentation describes sampling without replacement for sequences. For security-sensitive unique values, apply an appropriate uniqueness check while generating with secrets.

Choose the form that matches your requirement

  • Positive numeric value, exactly ten digits: use inclusive bounds from 1,000,000,000 through 9,999,999,999.
  • Exactly ten displayed characters, leading zeros allowed: generate from zero through 9,999,999,999 and apply :010d formatting.
  • Ordinary program behavior or simulation: use random.
  • Hard-to-predict security value: use secrets.
  • No repeated values: implement uniqueness handling; random generation alone does not guarantee it.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.