Numpy Tutorial
Creating NumPy Array
NumPy Array Manipulation
Matrix in NumPy
Operations on NumPy Array
Reshaping NumPy Array
Indexing NumPy Array
Arithmetic operations on NumPy Array
Linear Algebra in NumPy Array
NumPy and Random Data
Sorting and Searching in NumPy Array
Universal Functions
Working With Images
Projects and Applications with NumPy
The numpy.random.random()
function is used in the numpy
library to generate random floats in the half-open interval [0.0, 1.0). In essence, it's an alias for the numpy.random.random_sample()
function, which means they both provide the same functionality and can be used interchangeably.
numpy.random.random()
function tutorialFunction Signature:
numpy.random.random(size=None)
Parameters:
size
is not provided, a single float is returned.Examples:
import numpy as np random_float = np.random.random() print(random_float)
random_array = np.random.random(5) print(random_array)
random_matrix = np.random.random((3, 3)) print(random_matrix)
Generate random floats in a different range, say between 5 and 10:
To achieve this, you can use the following formula:
a, b = 5, 10 random_floats = (b - a) * np.random.random(5) + a print(random_floats)
This formula scales the random samples from the range [0, 1) to the range [a, b).
Note:
You can set a seed using numpy.random.seed(seed_value)
if you want reproducible results. This is useful especially when debugging or when you want consistent random values across multiple runs.
As mentioned before, numpy.random.random()
and numpy.random.random_sample()
are essentially the same in terms of functionality. The difference is just in the name.
In summary, numpy.random.random()
is a straightforward and useful function for generating random floating-point values. By using arithmetic operations, you can adjust and mold these values to fit different ranges and distributions as required for your applications.
Random sampling involves selecting random values from a distribution, and in Python, this can be achieved using NumPy's random()
function.
import numpy as np # Generate a random value between 0 and 1 random_value = np.random.random() # Print the random value print("Random Value:", random_value)
numpy.random.random()
for random sampling:Learn how to use NumPy's random()
function for generating random values.
import numpy as np # Generate a random value between 0 and 1 random_value = np.random.random() # Print the random value print("Random Value:", random_value)
random()
function for random value generation:Explore the usage of NumPy's random()
function, which generates random values in the half-open interval [0.0, 1.0).
import numpy as np # Generate a random value between 0 and 1 random_value = np.random.random() # Print the random value print("Random Value:", random_value)
numpy.random()
usage for random sampling:Learn how to use the numpy.random()
function in Python for random value sampling.
import numpy as np # Generate a random value between 0 and 1 random_value = np.random.random() # Print the random value print("Random Value:", random_value)
A sample code demonstrating random value sampling using NumPy in Python.
import numpy as np # Generate an array of 5 random values between 0 and 1 random_values = np.random.random(size=5) # Print the random values print("Random Values:", random_values)
Understand the process of generating random values using NumPy in Python.
import numpy as np # Generate an array of 3 random values between 0 and 1 random_values = np.random.random(size=3) # Print the random values print("Random Values:", random_values)
random
vs random_sample
for random sampling:Compare NumPy's random
with random_sample
for random value sampling.
import numpy as np import random # Using NumPy random random_values_np = np.random.random(size=5) # Using random_sample random_values_sample = np.random.random_sample(size=5) # Print the random values print("Random Values (NumPy):", random_values_np) print("Random Values (random_sample):", random_values_sample)
numpy.random.random()
for array randomization:Utilize numpy.random.random()
to randomize the order of elements in a NumPy array.
import numpy as np # Create an array to be randomized original_array = np.array([10, 20, 30, 40, 50]) # Randomize the order of elements using NumPy random randomized_array = np.random.random(size=len(original_array)) np.random.shuffle(randomized_array) # Print the randomized array print("Original Array:", original_array) print("Randomized Array:", randomized_array)