🧮

Random Number Generator

Generate random numbers within a custom range — integers or decimals, single values or lists, unique (no repeats) or with repetition allowed.

Loading…

Frequently Asked Questions

How does a random number generator work?

Computer-based random number generators (RNGs) fall into two categories. Pseudo-random number generators (PRNGs) — use a mathematical algorithm seeded with an initial value (the "seed") to produce sequences that appear random but are deterministic and reproducible. Common algorithms: Mersenne Twister (MT19937, used in Python, R, MATLAB), Xorshift, Linear Congruential Generator. Given the same seed, a PRNG always produces the same sequence — useful for reproducible experiments. JavaScript's Math.random() uses an implementation-defined PRNG. True random number generators (TRNGs) — harvest entropy from physical sources: atmospheric noise (Random.org), thermal noise, mouse movement, keystroke timing. These are genuinely unpredictable and are used in cryptography and secure key generation. For most everyday uses (games, sampling, simulations), PRNGs are indistinguishable from true randomness.

How do I generate a random number between 1 and 100?

The formula for a random integer in the range [min, max] is: Math.floor(Math.random() × (max − min + 1)) + min. For 1 to 100: Math.floor(Math.random() × 100) + 1. Math.random() returns a float in [0, 1) — it includes 0 but never reaches 1. Multiplying by (max − min + 1) scales to [0, 100). Math.floor() rounds down to the nearest integer [0, 99]. Adding min (1) shifts the range to [1, 100]. For a random float in [min, max]: Math.random() × (max − min) + min. Example: random float in [2.5, 7.5] = Math.random() × 5 + 2.5.

What is a random number seed and why does it matter?

A seed is the starting value that initialises a pseudo-random number generator (PRNG). Same seed → identical sequence every time. Different seeds → different sequences. Why seeds matter: Reproducibility in science — a researcher specifies the seed so others can reproduce the exact same "random" results: numpy.random.seed(42) in Python. Debugging — when testing code involving randomness, fixing the seed makes bugs reproducible. Gaming — some games use seeds to generate the same world for all players (e.g., Minecraft world seeds). Security — cryptographic applications must use unpredictable seeds (entropy from hardware) to prevent attackers from reproducing key sequences. Common reproducible seeds in examples: 42 (Douglas Adams joke, widely used in data science), 0, 123, 2024.

What are common uses of random number generators?

RNGs are fundamental across many fields. Statistics & research — random sampling from a population; assigning participants to treatment/control groups in experiments; bootstrapping and Monte Carlo simulation. Games & entertainment — dice rolling, card shuffling, procedural world generation (Minecraft, No Man's Sky), loot drops, NPC behaviour. Cryptography — generating encryption keys, session tokens, one-time passwords, SSL/TLS nonces. These require cryptographically secure RNGs (CSPRNGs). Computer science — randomised algorithms (quicksort random pivot), hash table randomisation, load balancing. Finance — Monte Carlo simulations for option pricing, portfolio stress testing, risk modelling. Education — generating unique exam questions, random group assignment, lottery draws. A simple use: randomly assign numbers 1–30 to 30 students for a class lottery.

What is the difference between random numbers with and without replacement?

With replacement (repetition allowed) — each number is drawn independently; the same value can appear multiple times. Analogous to rolling a die multiple times — 4 can appear on roll 1 and again on roll 3. Example: generating 5 numbers in [1, 10] with replacement might give: 3, 7, 3, 10, 3. Without replacement (unique) — once a number is drawn, it cannot be drawn again. Analogous to drawing cards from a shuffled deck without putting them back. Example: 5 unique numbers from [1, 10]: 3, 7, 1, 9, 5. Without replacement requires tracking previously drawn values and requires that you request no more numbers than are available in the range (you cannot draw 15 unique numbers from a range of 10). Without replacement is used for: lottery draws, random sampling surveys, raffle ticket selection, creating shuffled sequences. With replacement is used for: bootstrapping in statistics, dice/coin simulations, and any situation where independence between draws is required.