Quick Answer
Generating 1 random integer between 1 and 100 might return 42, 87, or 7. Every integer in the range has an equal probability of 1/100 = 1%.
Common Examples
| Input | Result |
|---|---|
| 1 to 100, 1 integer | A single random integer like 42 |
| 1 to 6, 2 integers | Two dice rolls like 3, 5 |
| 1 to 10, 5 integers | Five values like 7, 2, 9, 1, 4 |
| 0 to 1, 3 decimals | Three values like 0.4821, 0.1037, 0.9254 |
How It Works
The formula
For a random integer between min and max (inclusive):
n = floor(random() x (max - min + 1)) + min
Where random() produces a uniform value in [0, 1). The floor function and +1 ensure both endpoints are included with equal probability.
For a random decimal between min and max:
n = random() x (max - min) + min
This produces a uniformly distributed value anywhere in the continuous range [min, max).
Uniform distribution
Every value in the range has equal probability. For integers between 1 and 6, each number has a 1/6 (16.67%) chance of appearing. For integers between 1 and 100, each has a 1/100 (1%) chance. Over many trials, the frequency of each value converges toward this theoretical probability.
Pseudorandom numbers
Computers generate pseudorandom numbers using deterministic algorithms seeded with unpredictable values (system time, hardware entropy). The results pass statistical tests for randomness and are suitable for simulations, games, sampling, and general-purpose use. They are not suitable for cryptographic applications, which require a cryptographically secure random number generator (CSPRNG).
Worked example
To generate a random integer between 1 and 6 (simulating a die roll): random() returns, say, 0.7234. Multiply by (6 - 1 + 1) = 6 to get 4.3404. Floor that to 4, then add 1 to get 5. The result is 5, a valid die roll.
CalculateY