How do you generate a random decimal between two numbers?
Decide how many decimal places you need, which splits the range into equal steps, then pick one step at random. Between 0 and 1 at two decimal places there are 101 possible values, 0.00 to 1.00. This generator does exactly that, using your browser's secure random source, and prints each result with a fixed number of decimal places.
Settings
- Minimum and Maximum: any numbers, negative values and decimals included. The defaults are 0 and 1.
- Decimal places: a slider from 0 to 10, 2 by default. At 0 you get whole numbers.
- Numbers: how many values to generate, from 1 to 100.
- Click Generate. Results appear one per line.
A blank Minimum or Maximum uses the default, while a typed 0 is used as 0. If the minimum is bigger than the maximum, you get "Min must be less than or equal to max." instead of output.
Worked example: test temperatures
Suppose you're seeding a demo app with body-temperature readings between 35.5 and 38.5 °C at one decimal place. That range has 3.0 ÷ 0.1 = 30 steps, so 31 possible values, each equally likely. Six numbers from the tool looked like this:
36.2
36.5
37.0
36.9
37.0
36.0
Note the 37.0 and 36.0. The output always uses the exact number of places you chose (it's formatted with toFixed), which keeps CSV columns tidy and makes the values look like real instrument readings rather than JavaScript floats.
How the number is built
Floating-point maths is where most "random decimal" snippets go wrong. Math.random() * (max - min) + min gives values like 0.30000000000000004, never reaches the maximum, and needs rounding that quietly makes the two end values half as likely as the rest.
This tool works with whole numbers first:
- It turns both bounds into whole numbers of steps of 10^-places, rounding the minimum up and the maximum down so the grid never pokes outside the range.
- It picks a random integer between those two with rejection sampling over
crypto.getRandomValues(MDN reference), so every step has the same chance. - It divides by 10^places and formats the result.
Because the pick is an integer, every value on the grid, including both ends when they sit on it, is equally likely.
Precision versus range
The integer pick handles any number of steps, so wide ranges at high precision work fine. The real limit comes from how many digits a JavaScript number can print exactly: each bound multiplied by 10^places has to stay below about 4.5 quadrillion (2^52). In practice that means about 15 significant digits in total:
| Use case | Range | Places | Possible values | Works? |
|---|---|---|---|---|
| Prices | 0.99 to 99.99 | 2 | 9,901 | Yes |
| Latitude | −90 to 90 | 6 | 180,000,001 | Yes |
| Longitude | −180 to 180 | 7 | 3,600,000,001 | Yes |
| Probability | 0 to 1 | 9 | 1,000,000,001 | Yes |
| Probability | 0 to 1 | 10 | 10,000,000,001 | Yes |
| Large values | 1,000,000 to 1,000,001 | 10 | 10,000,000,001 | No, error message |
When you hit the limit, the tool says "Too many decimal places for numbers this large." Drop a decimal place (1,000,000 to 1,000,001 works at 9 places) or use smaller numbers.
A note on random coordinates: picking latitude uniformly between −90 and 90 crowds points towards the poles on a real globe, because a degree of latitude near the pole covers far less area than one at the equator. That's fine for testing a map widget, but not for simulating where things actually are.
Edge cases
- Decimal places 0 gives whole numbers. The random integer generator does the same with a count of up to 100 and much wider ranges.
- A minimum or maximum of 0 is used as 0, so −1 to 0 gives values from -1.00 to 0.00.
- Bounds with extra decimal places are trimmed inwards: 0.123 to 0.2 at 2 places returns 0.13 to 0.20, never 0.12.
- Text that isn't a number in either field gives "Min and max must be numbers."
Where random decimals help
Test fixtures are the main use: prices for an e-commerce demo, sensor readings for a dashboard, exchange rates, percentages, or GPS points to drop on a map. They're also useful for probability exercises, such as generating thresholds between 0 and 1, and for checking that your code rounds and formats numbers correctly. If you'd rather have exact ratios than decimals, the random fraction generator returns proper fractions in lowest terms, and the percentage calculator helps when the values are meant as percentages. Need whole rows of mock data? The random CSV generator builds a grid of random values ready for import tests.
Last updated