random number generation
probability
duplicate question
math
number range

Random number between 0 and 1?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Generating a random number between 0 and 1 sounds trivial, and in most languages it is a one-line operation. The part that actually matters is understanding which generator you are using, whether the interval includes 0 or 1, and whether the randomness is good enough for your use case.

What "Between 0 and 1" Usually Means

In programming, a random number "between 0 and 1" normally means a floating-point value in the half-open interval from 0 inclusive up to 1 exclusive. In other words, 0 can appear, but 1 typically cannot.

That convention is useful because it scales cleanly. If a generator gives you values in [0, 1), multiplying by 10 gives you [0, 10), which is easy to reason about for indexing, sampling, and probability thresholds.

Standard Pseudo-Random Generators

Most language-standard generators are pseudo-random number generators, not physical sources of randomness. They produce sequences that look random enough for simulations, games, shuffling, and Monte Carlo methods, but they are deterministic given the same internal state or seed.

That is perfectly fine for most application logic. It is not fine for secrets, tokens, or anything security-sensitive.

Common Language Examples

Python:

python
1import random
2
3value = random.random()
4print(value)

random.random() returns a float in [0.0, 1.0).

JavaScript:

javascript
const value = Math.random();
console.log(value);

Math.random() also returns a value in [0, 1).

Java:

java
1import java.util.Random;
2
3public class Demo {
4    public static void main(String[] args) {
5        Random random = new Random();
6        double value = random.nextDouble();
7        System.out.println(value);
8    }
9}

nextDouble() follows the same basic pattern.

Using the Result Correctly

A random float in [0, 1) is useful directly for probability checks:

python
1import random
2
3if random.random() < 0.2:
4    print("20 percent event happened")

That expression is common in simulations and randomized algorithms because the threshold maps directly to a probability.

You can also scale the result:

python
1import random
2
3value = random.random() * 5
4print(value)

Now the result falls in [0, 5).

Reproducibility Versus Fresh Randomness

In testing and scientific work, reproducibility is often more important than unpredictability. Seeding the generator makes the sequence repeatable:

python
1import random
2
3random.seed(42)
4print(random.random())
5print(random.random())

That is useful for debugging because the same "random" sequence appears every time. For real production randomness in simulations, you may skip the manual seed and let the runtime choose one.

Do Not Use Basic PRNGs for Secrets

This is the biggest practical distinction. random.random() and Math.random() are not appropriate for passwords, API tokens, session IDs, or cryptographic keys.

For security-sensitive values, use a cryptographically secure generator instead. In Python, for example:

python
1import secrets
2
3value = secrets.randbelow(10_000) / 10_000
4print(value)

That example shows the idea, but for actual secret generation you usually want bytes or tokens rather than a floating-point number.

Common Pitfalls

The most common mistake is assuming the generator can return 1 exactly. In many standard APIs, it cannot, because the interval is half-open.

Another issue is using a standard pseudo-random generator for security work. A generator that is fine for simulation can be completely wrong for authentication or token generation.

Developers also forget that seeded pseudo-random generators are deterministic. That is a feature for tests, but it is a weakness for security.

Summary

  • Most standard APIs return a pseudo-random float in the interval [0, 1).
  • That half-open range is useful for scaling and probability comparisons.
  • Use built-in random generators for simulations, sampling, and general-purpose logic.
  • Seed the generator when reproducibility matters.
  • Use a cryptographically secure generator instead for secrets or security-sensitive values.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.