Random Number Generator: How to Get One

How-to basics · Updated November 23, 2026

Yes, here is a random number: 42. But if you need a random number for a specific purpose—like a lottery draw, a password, or a statistical sample—you need a reliable method to generate one, and that’s what this article covers.

What Is a Random Number, Really?

A random number is a value chosen from a set of possible values in such a way that each value has an equal (or specified) probability of being selected, and the selection of one value does not influence the selection of any other. In everyday use, people often mean “a number I can’t predict,” but true randomness is a mathematical property, not just a feeling of surprise.

There are two broad categories: true randomness and pseudo-randomness. True randomness comes from physical processes that are inherently unpredictable, like radioactive decay or atmospheric noise. Pseudo-randomness comes from algorithms that produce sequences that look random but are actually deterministic—if you know the starting point (the seed), you can reproduce the exact sequence.

How Random Number Generators Work

Most random numbers you encounter—from a spreadsheet’s RAND function to a video game’s loot drop—are generated by pseudo-random number generators (PRNGs). These algorithms take an initial value (the seed) and apply mathematical operations to produce a sequence of numbers that pass statistical tests for randomness. Common PRNGs include:

  • Linear congruential generators (LCGs) – simple, fast, but not cryptographically secure
  • Mersenne Twister – widely used in scientific computing, has a long period
  • Xorshift – fast and simple, used in many game engines

For security-sensitive applications (like generating encryption keys), cryptographically secure PRNGs (CSPRNGs) are used. These are designed to be unpredictable even if an attacker knows the algorithm and some output. They often rely on system entropy sources—like mouse movements, network traffic, or hardware noise—to seed the generator.

True random number generators (TRNGs) use physical phenomena. For example, random.org uses atmospheric noise to generate random numbers. These are considered truly random because the underlying process is not deterministic.

How to Generate a Random Number Yourself

You don’t need special software to get a random number. Here are several practical methods:

  1. Use a spreadsheet – In Microsoft Excel, type =RAND() to get a decimal between 0 and 1. To get an integer between two values, use =RANDBETWEEN(bottom, top), e.g., =RANDBETWEEN(1,100) gives a whole number from 1 to 100. In Google Sheets, the same functions work.
  2. Use a programming language – Python’s random.randint(1,100) or JavaScript’s Math.floor(Math.random() * 100) + 1 are common.
  3. Use a physical method – Flip a coin for binary choices, roll dice for 1–6, or draw numbered slips from a hat. These are truly random if done properly.
  4. Use an online service – Websites like random.org provide true random numbers based on atmospheric noise.

For a quick mental trick, you can use the current time: take the seconds from your watch and use the last digit—but this is not truly random, as you can predict it.

Common Problems with Random Number Generation

One common misconception is that a PRNG is “random enough” for everything. But if you use the same seed, you get the same sequence—this is useful for reproducibility in simulations, but disastrous if you’re using it for a lottery. Many programming languages automatically seed with the current time, which makes the sequence unpredictable to a casual observer, but a determined attacker could guess the seed if they know the approximate time.

Another issue is modulo bias. If you use Math.random() (which returns a float between 0 and 1) and multiply by 100 and round down, you get numbers 0–99, but each number may not be equally likely due to floating-point precision. In practice, for most non-critical uses, the bias is negligible, but for rigorous statistical work, you should use a proper sampling algorithm.

Also, human-generated “random” numbers are notoriously non-random. When people are asked to pick a number between 1 and 10, they disproportionately choose 7. So if you need randomness, avoid relying on your intuition.

How to Choose the Right Random Number Generator

The choice depends on your use case:

  • For casual use (e.g., picking a winner from a hat): a simple PRNG like RANDBETWEEN is fine.
  • For simulations or statistical sampling: use a PRNG with a long period and good statistical properties, like the Mersenne Twister (available in Python’s random module, R, and many other tools).
  • For security (passwords, tokens, encryption keys): use a CSPRNG, such as secrets in Python, crypto.getRandomValues() in JavaScript, or RNGCryptoServiceProvider in .NET.
  • For gambling or lottery draws: use a true random source, such as a hardware RNG or an atmospheric noise service, to ensure fairness.

If you need to generate many random numbers, consider the period of the PRNG—the point at which the sequence repeats. The Mersenne Twister has a period of 2^19937 − 1, which is astronomically large, so it won’t repeat in practice.

Cost and Availability

Generating random numbers is essentially free. Spreadsheet functions, programming languages, and most online services have no cost. Some true random number services, like random.org, offer free limited use, but charge for high-volume or commercial use. If you need hardware-based randomness, a dedicated USB RNG device can cost anywhere from $20 to several hundred dollars, but for most people, software solutions are sufficient.

Safety and Security Considerations

When using random numbers for security, never use a non-cryptographic PRNG. For example, don’t use Math.random() to generate a password reset token, as it is predictable. Also, be cautious with seeds: if you seed a PRNG with a fixed value, the output is predictable, so never do that for security purposes.

For password generation, use a dedicated password manager or a CSPRNG. For example, in Python, you can use secrets.token_hex(16) to get a 16-byte random hex string. In JavaScript, crypto.getRandomValues(new Uint32Array(1))[0] gives a random 32-bit integer.

If you’re using random numbers for statistical sampling, ensure your sample is unbiased. A common mistake is to use a PRNG that hasn’t been properly seeded, leading to correlated samples. Always seed with a good entropy source, such as the system time combined with other system data.

Frequently Asked Questions

Can I generate a random number in Excel?

Yes. Use =RAND() for a decimal between 0 and 1, or =RANDBETWEEN(bottom, top) for an integer. For example, =RANDBETWEEN(1,100) gives a random integer from 1 to 100. Note that these functions recalculate whenever the sheet changes, so if you need a static number, copy and paste as values.

Is a random number from a computer truly random?

Most computer-generated numbers are pseudo-random, meaning they are generated by an algorithm and are not truly random. However, they are designed to pass statistical tests for randomness, so for most purposes they are indistinguishable from true randomness. For security-critical uses, you need a cryptographically secure generator, which is seeded with unpredictable data.

How do I get a random number between 1 and 10?

In Excel, use =RANDBETWEEN(1,10). In Python, use random.randint(1,10). In JavaScript, use Math.floor(Math.random() * 10) + 1. Or simply roll a 10-sided die (available at game stores) for a physical method.

What is the most random number?

There is no “most random” number—randomness is about the process, not the value. However, the number 42 is often cited as a joke (from Douglas Adams’ The Hitchhiker’s Guide to the Galaxy), but it is no more random than any other number. If you need a random number, any value from the set is equally likely.

Can I use my phone to generate a random number?

Yes. Many calculator apps have a random number function, and there are numerous dedicated random number generator apps. Also, you can use a web browser to visit random.org or use a search engine’s built-in random number feature (e.g., Google’s “random number generator” widget).

Why is 7 the most common random number people choose?

Psychological studies have shown that when people are asked to pick a number between 1 and 10, they disproportionately choose 7. This is likely due to cultural associations (lucky number) and cognitive biases. This is why it’s important to use a mechanical or algorithmic method for true randomness.

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.