To create a 6-digit random number, use a random number generator that produces integers from 100000 to 999999 inclusive. For example, in Python, random.randint(100000, 999999) does this; in Excel, =RANDBETWEEN(100000,999999) works; and in JavaScript, Math.floor(100000 + Math.random() * 900000) gives the same range. This answer covers why the range matters, how to generate such numbers across common tools, and practical considerations for security and uniqueness.
Why 100000 to 999999, Not 000000 to 999999
A 6-digit number, by common definition, has six digits with the first digit not being zero. That means the smallest valid number is 100000 and the largest is 999999. If you include 000000, you get a 1-digit number (zero) padded with zeros, which is not a true 6-digit number. For most purposes—like OTP codes, lottery picks, or random IDs—people expect the first digit to be non-zero. However, if you need a 6-character string that can include leading zeros (e.g., for a code like “004271”), then you should generate a number from 0 to 999999 and pad it with leading zeros. The distinction matters: a random integer from 0 to 999999 has 1,000,000 possible values, while one from 100000 to 999999 has 900,000 possible values.
Generating a 6-Digit Random Number in Excel
In Microsoft Excel (Microsoft 365 on Windows), the simplest way is to use the RANDBETWEEN function. In any cell, type:
=RANDBETWEEN(100000, 999999)
This recalculates every time the worksheet changes. To generate a list, copy the formula down. If you want static numbers that don’t change, copy the cells and paste as values (right-click > Paste Special > Values). For a version that uses the newer RANDARRAY function (available in Excel 365), you can generate multiple numbers at once. For example, to create 10 random 6-digit numbers in cells A1:A10, enter:
=RANDARRAY(10,1,100000,999999,TRUE)
This returns an array of 10 integers. The TRUE argument forces integers. On Mac, the same formulas work. In Excel for the web, they also work. If you need a 6-character string with leading zeros, use:
=TEXT(RANDBETWEEN(0,999999),"000000")
This produces text like “004271”. Note that the result is text, not a number, so you cannot use it in arithmetic without converting it back.
Generating a 6-Digit Random Number in Python
Python’s random module is the standard tool. The most direct method:
import random
number = random.randint(100000, 999999)
print(number)
This gives a uniform random integer in that range. If you need a 6-character string with leading zeros, use:
import random
code = f"{random.randint(0, 999999):06d}"
print(code)
The :06d format pads with zeros to six digits. For cryptographic security (e.g., for a password or OTP), never use the random module; use secrets instead:
import secrets
secure_number = secrets.randbelow(900000) + 100000
print(secure_number)
This uses a cryptographically secure random source. The random module is not suitable for security because it is deterministic and predictable.
Generating in JavaScript (Browser or Node.js)
In a browser, the built-in Math.random() is common but not cryptographically secure. To get a 6-digit integer:
let num = Math.floor(100000 + Math.random() * 900000);
console.log(num);
This works because Math.random() returns a float from 0 (inclusive) to 1 (exclusive). Multiplying by 900000 gives a range from 0 to just under 900000; adding 100000 shifts it to 100000 to just under 1000000; Math.floor rounds down to an integer, giving 100000 to 999999. For secure random numbers in a browser, use the Web Crypto API:
let array = new Uint32Array(1);
window.crypto.getRandomValues(array);
let num = (array[0] % 900000) + 100000;
console.log(num);
Note that modulo bias exists if the range does not evenly divide the maximum, but for 900000 and 2^32, the bias is negligible for most purposes. In Node.js, use crypto.randomInt():
const crypto = require('crypto');
let num = crypto.randomInt(100000, 1000000); // exclusive upper bound
console.log(num);
The upper bound is exclusive, so 1000000 gives 100000 to 999999.
How to Choose a Method: Security vs. Speed
Decide based on what the number is for. If it’s for a game, a lottery simulation, or a random sample in a non-sensitive context, Math.random() or random.randint() is fine. If it’s for a password reset code, a two-factor authentication OTP, or any security-sensitive token, you must use a cryptographically secure generator. The difference is not just about how “random” the number looks; it’s about whether an attacker can predict future numbers by observing past ones. The random module in Python and Math.random() in JavaScript are pseudo-random number generators (PRNGs) that are seeded with a predictable value (often the current time). An attacker who can observe a few outputs can potentially reconstruct the seed and predict the next numbers. Secure generators use OS-level entropy and are designed to be unpredictable.
Common Problems and Misconceptions
- “The number repeats often.” With 900,000 possible values, the probability of a repeat in a small sample is low but not zero. The birthday paradox says you need about 1,000 numbers to have a ~50% chance of a duplicate. If you need unique numbers, use a counter or a database constraint, not randomness.
- “Leading zeros are lost.” When you generate an integer, 100000 is stored as 100000, not as “100000” with a leading zero. If you need a fixed-length code, treat it as text and pad with zeros.
- “RANDBETWEEN recalculates too often.” In Excel, any change to the sheet triggers recalculation. To freeze the values, copy and paste as values.
- “The number is not truly random.” No computer generates true randomness without specialized hardware. PRNGs are deterministic, but for most uses, they are statistically indistinguishable from true randomness. Only use secure generators when security matters.
- “I can use the same number twice.” If you need a unique ID, randomness alone is not enough. Use a combination of timestamp and random, or a sequential number, or a UUID.
Alternatives: UUIDs, Timestamps, and Sequential Numbers
Sometimes a 6-digit random number is not the best choice. For a unique identifier, a 6-digit space (900,000 possibilities) is too small for many systems. A UUID (Universally Unique Identifier) is a 128-bit number, often shown as 36 characters, and is practically unique across all systems. In Python, use uuid.uuid4(); in JavaScript, use crypto.randomUUID(); in Excel, there is no built-in UUID function, but you can use a VBA macro or an online generator. For a short code that is unique within a day, you might combine a date-based prefix with a random suffix, like “20250321-123456”. For sequential numbers, a simple counter is deterministic and guaranteed unique, but it is predictable, so it is not suitable for security tokens.
Frequently Asked Questions
How do I generate a 6-digit random number without repeats?
Randomness does not guarantee uniqueness. To get a set of non-repeating 6-digit numbers, generate a list of all numbers from 100000 to 999999, shuffle it, and take the first N. In Python, use random.sample(range(100000, 1000000), N). In Excel, you can sort a column of random numbers and then take the corresponding numbers, but it’s easier to use a formula with RAND() and a helper column, or use a VBA macro. For small sets (like 10 numbers), the chance of a repeat is low, but not zero.
What is the difference between RAND() and RANDBETWEEN() in Excel?
RAND() returns a decimal between 0 and 1 (e.g., 0.54321). RANDBETWEEN(bottom, top) returns an integer between the two specified numbers. To get a 6-digit number with RAND(), you would need to multiply and round, like =ROUND(RAND()*899999+100000,0), but RANDBETWEEN is simpler and clearer.
Can I use a 6-digit random number for a password?
Technically yes, but it is extremely insecure. A 6-digit number has only 900,000 possible combinations, which a computer can brute-force in seconds. For passwords, use at least 12 characters with a mix of letters, numbers, and symbols. For OTPs, 6-digit codes are common but are usually time-based and expire quickly, and they are often combined with other factors. Never use a 6-digit random number as a standalone password for anything important.
How do I generate a 6-digit random number in Google Sheets?
Google Sheets uses the same RANDBETWEEN function: =RANDBETWEEN(100000, 999999). To generate a list, drag the fill handle. To get a static value, copy and paste as values (Edit > Paste special > Values only). Google Sheets also has RANDARRAY in newer versions, but it may not be available in all regions; the simplest is to use RANDBETWEEN in each cell.
Why does my random number sometimes have fewer than 6 digits?
If you generate a number from 0 to 999999 and display it without leading zeros, numbers like 12345 will show as 5 digits. To force 6 digits, format the cell as text with leading zeros (in Excel, use =TEXT(...,"000000")) or use a custom number format 000000 on the cell. In Python, use the :06d format. In JavaScript, use String(num).padStart(6, '0').
Is there a way to generate a 6-digit random number in Excel that doesn’t change?
Yes. After entering =RANDBETWEEN(100000,999999), copy the cell, then right-click and choose Paste Special > Values. This replaces the formula with the static number. Alternatively, you can set calculation to manual (Formulas > Calculation Options > Manual) and press F9 to recalculate only when you want, but the numbers will still change when you recalculate. The paste-as-values method is the most reliable.
Related
- How to Generate True Random Numbers
- Shortcut for File Explorer: Best Ways to Open It Fast
- Tableau String to Number Conversion: The 3 Best Ways
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- F Statistic in Excel: How to Find ItTo find an F-statistic in Excel, arrange your data in columns, enable the Analysis ToolPak if needed, then…
- Excel Page Break Dotted Lines: Meaning and Fixes ExplainedExcel page break dotted lines mark where the worksheet is expected to split across printed pages. To hide…
- Add Page Numbers in Microsoft Word: A Practical GuideTo add page numbers in Microsoft Word for Microsoft 365 on Windows, select Insert > Page Number, choose…
- MS Word Data Table: How to Create, Format, and Use ItIn Microsoft Word for Microsoft 365, make a data table by choosing Insert > Table, then selecting the…
- Excel Data Validation Restrictions and Their LimitsExcel data validation can restrict what users enter in selected cells—for example, to a number range, a date…
- Default Template in Word: How to Set It UpTo make new blank documents start with your preferred formatting, edit Word’s Normal.dotm template, save it, then open…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.