NumPy random.random_sample(): Uniform Random Floats in [0.0, 1.0)

import numpy as np

np.random.seed(42)
samples = np.random.random_sample(size=(2, 3))
print(samples)

numpy.random.random_sample(size=None)
random_sample() (also available via aliases np.random.random(), np.random.ranf(), and np.random.sample()) returns random floats in the continuous interval [0.0, 1.0).

The random_sample() function is fundamental for generating uniformly distributed random numbers, simulations, weight initializations in machine learning, and stochastic modeling. For random integers, see randint(). For normal distributions, see randn().

Parameter Description
size Optional; int or tuple of ints (e.g., (3, 4)). If None (default), a single scalar float is returned.

Syntax & Aliases 🔝

numpy.random.random_sample(size=None)
numpy.random.random(size=None)
numpy.random.ranf(size=None)
numpy.random.sample(size=None)

Legacy vs. Modernized Code Comparison 🔝

The table below compares legacy NumPy random snippets with the updated, structured implementation:

Aspect Legacy Implementation Modernized Implementation
Structure & Markup Basic echo output with unformatted pre tags and missing metadata. Structured HTML5 layout, Schema TechArticle markup, and responsive tables.
Code Presentation Requires scrolling past introductory text to find code examples. Core code snippet placed right at the top for instant visitor access.
Verification Unchecked print statements with manual result inspection comments. Automated unit test assertions (assert, np.allclose) verifying shape and bounds.
Ecosystem Integration Isolated code blocks with sparse navigation links. Comprehensive cross-linking, quick-jump navigation, and runnable Colab notebooks.

Generating 1D and Multi-Dimensional Arrays 🔝

Pass shape tuples to size to generate matrices of uniform random numbers:

import numpy as np

np.random.seed(100)

# 1D array of 5 floats
arr_1d = np.random.random_sample(size=5)
print("1D array:", arr_1d)

# 2D matrix (3 rows, 2 columns)
arr_2d = np.random.random_sample(size=(3, 2))
print("2D matrix:
", arr_2d)

assert arr_1d.shape == (5,)
assert arr_2d.shape == (3, 2)
assert np.all(arr_1d >= 0.0) and np.all(arr_1d < 1.0)

Expected output

1D array: [0.54340494 0.27836939 0.42451758 0.84477613 0.00471886]
2D matrix:
 [[0.12156912 0.67074904]
 [0.82585276 0.13670656]
 [0.57504433 0.89132198]]

Function Aliases: random, ranf, sample 🔝

NumPy provides identical functional aliases for random_sample(). np.random.random() is especially common in machine learning codebases:

import numpy as np

np.random.seed(42)

a = np.random.random(3)
b = np.random.ranf(3)
c = np.random.sample(3)

print("random():", a)
print("ranf():  ", b)
print("sample():", c)

assert np.array_equal(a, b) and np.array_equal(b, c)

Expected output

random(): [0.37454012 0.95071431 0.73199394]
ranf():   [0.37454012 0.95071431 0.73199394]
sample(): [0.37454012 0.95071431 0.73199394]

Scaling to Custom Ranges [Low, High) 🔝

To generate uniform random floats across an arbitrary range [low, high), multiply the standard [0.0, 1.0) samples by the range width and add the lower bound:

low, high = 5.0, 15.0
scaled = (high - low) * np.random.random_sample(4) + low
print("Scaled floats in [5, 15):", scaled)

assert np.all(scaled >= low) and np.all(scaled < high)

Expected output

Scaled floats in [5, 15): [ 8.74540119 14.50714306 12.31993936  6.15858262]

Modern Generator: default_rng().random() 🔝

In modern NumPy (v1.17+), the recommended approach uses the Philox/PCG64 bit generator via default_rng() rather than legacy global state functions:

rng = np.random.default_rng(seed=42)
modern_samples = rng.random(size=(2, 2))
print("Modern Generator samples:
", modern_samples)

assert modern_samples.shape == (2, 2)

Expected output

Modern Generator samples:
 [[0.77395605 0.43887844]
 [0.85859792 0.69736803]]

Common Pitfalls & Best Practices 🔝

  • Exclusive Upper Bound: Remember that values generated are strictly less than 1.0 (interval is [0.0, 1.0)).
  • Shape Tuple Syntax: When requesting multi-dimensional arrays, pass dimensions as a tuple size=(rows, cols) rather than separate positional arguments.

Practice in Google Colab 🔝

Test random_sample() arrays, function aliases, custom range scaling, modern generators, and run verification test suites in our interactive Google Colab companion notebook:

Interactive Practice: Colab & GitHub

Launch the notebook directly in Google Colab to run live code cells or inspect the source repository on GitHub:

View on GitHub Open In Colab

Continue exploring related NumPy random functions: randn(), randint(), and NumPy Overview.




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer