Content · Programming · Python

Python random.random() Function: Usage and Examples

Generate a pseudorandom floating-point number in a half-open interval.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

Generate a pseudorandom floating-point number in a half-open interval.

What It Does and Why It Matters


random.random() returns a float that is at least 0.0 but strictly less than 1.0. It is generated pseudorandomly, and an explicitly seeded Random instance can give reproducible tests.

Use secrets for authentication tokens and random.Random(seed) for reproducible simulations.

Pseudorandom examples are suitable for simulation and tests. Use secrets instead for tokens, passwords and authentication.

Step-by-Step Example


Start with the complete example below. Read the inputs and predict what the final print statement will show before you run it.

import random
random_generator = random.Random(42)
random_value = random_generator.random()
print(0.0 <= random_value < 1.0)

How the Code Works


  • The imports make these standard-library tools available: random.
  • The name random_generator receives the value of random.Random(42). Changing this input lets you explore a different case.
  • The print calls display the results. Compare these results with the method’s documented return type and the input used.
  • The dedicated Random instance isolates the generator state from unrelated random calls. A fixed seed is useful here for repeatable demonstrations but must not be reused to generate secrets.

Expected output:

True

For the demonstrated inputs, Python displays 'True'. This result follows from the operation described above; change one input at a time to understand which rule matters.

Second Example: Compare Two Cases


Use a different input or a related operation to observe an important difference. The two examples help separate the general rule from one particular result.

import random
rng=random.Random(21)
print(0 <= rng.random() < 1)

Expected output:

True

Where You Would Use This


Use this in calculations, data summaries, simulations or validation when the numerical assumptions match the task. Check input domains, units and precision before interpreting a result. A single example output does not prove a statistical conclusion.

Practice Exercise


Generate a hundred random values and verify they lie in the documented range.

Before running your edited example, write down the result you expect. Then test at least one valid boundary or deliberately invalid value, using appropriate exception handling where necessary.

Reference: official Python documentation for the complete API contract and version notes.

Common Beginner Mistakes


  • Assuming random.random() can return exactly 1.0 or is safe for password generation.
  • Using a numeric result without checking empty data, invalid ranges, precision and the meaning of its units.

Key Takeaways


  • Generate a pseudorandom floating-point number in a half-open interval.
  • random.random() returns a float that is at least 0.0 but strictly less than 1.0.
  • Generate a hundred random values and verify they lie in the documented range.
  • Pseudorandom examples are suitable for simulation and tests. Use secrets instead for tokens, passwords and authentication.
Share this resourceSend this page to someone who may find it useful.