Generate Random Number String: How to Do It

How-to basics · Updated November 22, 2026

To generate a random number string, use a dedicated random string generator tool, a programming language’s built-in random functions, or a spreadsheet formula like =RANDBETWEEN(100000,999999) in Excel. The best method depends on your context—whether you need a one-off value, a large batch, or a cryptographically secure sequence.

What Is a Random Number String?

A random number string is a sequence of digits (e.g., “482913”) produced by a process that has no predictable pattern. Unlike a single random number, a string implies a fixed length and often a specific format—like a 6-digit verification code or a 16-digit card number. The randomness can come from physical sources (like radioactive decay) or algorithmic pseudo-randomness.

Most software uses pseudo-random number generators (PRNGs), which start from a seed value and apply a deterministic algorithm. They appear random but are reproducible if the seed is known. For security-sensitive uses (passwords, tokens), you need a cryptographically secure PRNG (CSPRNG) that uses system entropy.

Why You Might Need a Random Number String

  • Verification codes (e.g., 2FA codes, SMS OTPs)
  • Password reset links (often alphanumeric, but sometimes numeric)
  • Lottery or contest draws
  • Database primary keys (when sequential IDs are undesirable)
  • Test data for software development

Each use case has different requirements. For a lottery, fairness and unpredictability matter. For a database key, uniqueness and performance matter. For an OTP, security is paramount—so you’d avoid a simple RAND() in Excel.

How to Generate a Random Number String

There are several practical methods, depending on your tools.

Using a Web-Based Generator

Many websites offer free random number generators. They typically let you specify the length and the number of strings. For example, random.org uses atmospheric noise for true randomness. However, for sensitive data, be cautious about transmitting values over the internet.

Using a Programming Language

In Python, you can use the random module for non-secure needs:

import random
print(''.join(str(random.randint(0,9)) for _ in range(6)))

For a secure version, use secrets:

import secrets
print(''.join(str(secrets.randbelow(10)) for _ in range(6)))

In JavaScript (Node.js), use crypto.randomInt():

const crypto = require('crypto');
let num = '';
for (let i = 0; i < 6; i++) {
  num += crypto.randomInt(0, 10);
}
console.log(num);

In Excel, you can generate a fixed-length random number with a formula. For a 6-digit string (including leading zeros), use:

=TEXT(RANDBETWEEN(0,999999),"000000")

This assumes you want a text string. If you need a number, use =RANDBETWEEN(100000,999999) (this excludes numbers with fewer than 6 digits).

How to Choose the Right Method

Consider these factors:

  • Security level: For passwords, tokens, or anything a malicious actor could exploit, use a CSPRNG (e.g., secrets in Python, crypto.getRandomValues in browser JavaScript, or RANDARRAY in Excel? No—Excel’s RAND is not secure).
  • Volume: For thousands of strings, a script is faster than a website.
  • Formatting: Do you need leading zeros? A specific length? A range (e.g., 1000-9999)?
  • Integration: Are you working within a spreadsheet, a web app, or a command line?

For Excel, the RANDBETWEEN function recalculates every time the sheet recalculates (press F9). If you need a static list, copy and paste as values. For a dynamic array of random numbers, use =RANDARRAY(rows, columns, min, max, TRUE) (Excel 365/2021). Example: =RANDARRAY(10,1,100000,999999,TRUE) generates 10 random 6-digit numbers in A1:A10.

Common Pitfalls and Misconceptions

Misconception: “Random” means “unique.” Random strings can repeat. If you need uniqueness, you must check for duplicates or use a UUID (which is practically unique but not numeric-only).

Pitfall: Using a non-secure generator for security. Excel’s RAND() and Python’s random are not cryptographically secure. An attacker who knows the seed or the algorithm could predict the output.

Pitfall: Forgetting leading zeros. If you need a fixed-length string, like “000123”, a numeric generator will drop the zeros. Use the TEXT function in Excel or string formatting in code.

Pitfall: Relying on a website for sensitive data. The website could log your generated numbers. For security, generate locally.

Security Considerations

If you’re generating a random number string for authentication, use a CSPRNG. In Python, the secrets module is designed for this. In JavaScript, use crypto.getRandomValues() (in browsers) or crypto.randomInt() (in Node.js). In Excel, there is no built-in secure random function, so you should not use Excel for generating passwords or tokens.

Also, consider the length. A 6-digit code has 1,000,000 possibilities. An attacker can try many guesses, so add rate limiting. For higher security, use at least 8 digits or a mix of letters and numbers.

Cost and Availability

Most methods are free. Web generators are free but may have rate limits. Programming libraries are free. Excel’s functions are included in your license. There is no inherent cost, but if you need a large batch of truly random numbers (e.g., for a lottery), you might pay for a service like random.org’s API.

Alternatives to Random Number Strings

If you need a unique identifier, consider:

  • UUIDs (e.g., 550e8400-e29b-41d4-a716-446655440000) – not numeric-only, but universally unique.
  • Sequential numbers – simple but predictable.
  • Timestamp-based numbers – e.g., 20250314123045 – unique if you add a counter.
  • Hash of a counter – e.g., SHA-256 of a sequence number, truncated.

For non-security uses, a simple counter or timestamp might be enough.

Frequently Asked Questions

How do I generate a random number string in Excel without duplicates?

Excel’s RANDBETWEEN can produce duplicates. To generate a list of unique random numbers, you can use a helper column with =RAND() and then rank them, or use the RANDARRAY function combined with UNIQUE (Excel 365). For example, =UNIQUE(RANDARRAY(100,1,100000,999999,TRUE)) will return unique random numbers, but it may return fewer than 100 if duplicates occur. For a guaranteed set, use a loop in VBA.

Is there a way to generate a random number string in Google Sheets?

Yes. Use =RANDBETWEEN(100000,999999) for a 6-digit number (no leading zeros). For a string with leading zeros, use =TEXT(RANDBETWEEN(0,999999),"000000"). Google Sheets also has =RANDARRAY() (available in newer versions) to generate multiple numbers. Note that Google Sheets’ random functions are not cryptographically secure.

Can I generate a random number string in Notepad or a text editor?

No, a plain text editor cannot generate random numbers. You need a calculator, a programming language, or a dedicated tool. You could use the command line in Windows (PowerShell) or macOS (Terminal) to generate random numbers.

What is the difference between a random number and a random number string?

A random number is a numeric value, like 42. A random number string is a sequence of digits treated as text, which can include leading zeros, e.g., “000042”. Strings are useful when the length must be fixed or when the value is used as an identifier rather than for arithmetic.

How can I generate a random number string in Python that is cryptographically secure?

Use the secrets module. For a 6-digit string with leading zeros, you can do: secrets.choice('0123456789') repeated six times and joined. Or use secrets.randbelow(10) for each digit. This uses the system’s secure random source.

Why does my random number string sometimes have fewer digits than I asked for?

If you generate a number and then convert it to a string, leading zeros are dropped. For example, str(random.randint(0,999999)) could yield “123” instead of “000123”. To keep the length, format the string with leading zeros (e.g., f"{num:06d}" in Python).

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.