Random Integer Generator
Generate random integers in code without the classic off-by-one and bias bugs.
The correct way to get a random integer from min to max in JavaScript is Math.floor(Math.random() * (max - min + 1)) + min, and in Python it is random.randint(min, max). Both include both endpoints. For security-sensitive uses, switch to crypto.getRandomValues or Python's secrets module.
JavaScript: the correct formula
The standard recipe: Math.floor(Math.random() * (max - min + 1)) + min. The +1 is what makes max reachable; without it you get min to max-1, the classic off-by-one bug. Math.floor (not Math.round) keeps the distribution uniform, because Math.round would make the endpoints half as likely as interior values.
For unbiased cryptographic integers, use crypto.getRandomValues with rejection sampling: draw a 32-bit value, discard draws in the biased remainder zone, and take the remainder modulo the range size. This tool's generator does exactly that.
Python: randint and secrets
Python's random.randint(a, b) returns an integer from a to b inclusive, both endpoints included, which already avoids the off-by-one trap. random.randrange(a, b+1) is the equivalent with exclusive-upper-bound semantics.
For tokens, passwords, or anything security-related, use the secrets module: secrets.randbelow(n) returns 0 to n-1 from the OS entropy source. Never use the random module for security purposes; its Mersenne Twister state is predictable.
Avoiding modulo bias
The naive pattern rand() % range slightly favors small numbers whenever the range does not divide the generator's output space evenly. With a 32-bit source and a range like 100, the bias is tiny but real; with small sources like a byte (0-255) and a range like 100, it is significant.
Rejection sampling fixes it: compute the largest multiple of the range that fits in the output space, discard any draw at or above it, and take the modulo of the rest. Every kept value is then exactly equally likely.
Seeding and reproducibility
Sometimes you want the same 'random' sequence every run: procedural game worlds, reproducible simulations, test fixtures. Seed a pseudorandom generator explicitly, Python's random.seed(42) or a seeded JS library, and document the seed. Math.random cannot be seeded, which is one more reason to pick your generator deliberately.
Keep seeded and unseeded uses apart. A simulation seed is a feature; a predictable session token is a vulnerability. Use separate generators for separate jobs.
Skip the arithmetic
Need numbers without writing code? Use the free random number generator.
Integer generation questions
How do I get a random integer between min and max in JavaScript?
Use Math.floor(Math.random() * (max - min + 1)) + min. The + 1 makes the maximum reachable (without it you get max - 1 at most), and Math.floor keeps every value equally likely, unlike Math.round which underweights the endpoints.
What is Python's random integer function?
random.randint(a, b) returns a random integer N with a <= N <= b, both endpoints included. For security-sensitive uses like tokens, switch to the secrets module: secrets.randbelow(n) draws from the operating system's entropy source.
What is modulo bias?
Modulo bias is the slight favoritism toward small numbers that appears when you take a random value modulo a range that does not divide the generator's output space evenly. Rejection sampling, discarding draws in the uneven leftover zone, eliminates it.