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

Random sampling in numpy | random_sample() function

numpy.random.random_sample() is a function in the numpy library that returns random floats in the half-open interval [0.0, 1.0). It can be very useful when you need continuous random samples.

numpy.random.random_sample() function tutorial

Function Signature:

numpy.random.random_sample(size=None)

Parameters:

  • size: The shape of the output. It could be an integer or a tuple of integers. If size is not provided, a single float is returned.

Examples:

  • Generate a single random float between 0 and 1:
import numpy as np
random_float = np.random.random_sample()
print(random_float)
  • Generate an array of 5 random floats between 0 and 1:
random_array = np.random.random_sample(5)
print(random_array)
  • Generate a 3x3 matrix of random floats between 0 and 1:
random_matrix = np.random.random_sample((3, 3))
print(random_matrix)
  • Generate random floats in a different range, say between 5 and 10: To achieve this, you can use a simple formula:

    a, b = 5, 10
    random_floats = (b - a) * np.random.random_sample(5) + a
    print(random_floats)
    

    This formula scales the random samples from the range [0, 1) to the range [a, b).

Alternative Functions:

The following functions provide similar functionality:

  • numpy.random.rand(): This function also generates random samples from a uniform distribution over [0, 1), but its syntax is slightly different.
  • numpy.random.random(): An alias to random_sample.
  • numpy.random.ranf(): Another alias to random_sample.

Note: For reproducibility, you might want to use numpy.random.seed(seed_value) to set a seed for the random number generator.

The numpy.random.random_sample() function is valuable when you need to generate random floating-point samples quickly. Combine it with arithmetic operations to adapt it for various ranges and distributions.

1. Random sampling in Python with NumPy:

Random sampling involves selecting random values from a distribution, and in Python, this can be achieved using NumPy's random_sample() function.

import numpy as np

# Generate a random value between 0 and 1
random_value = np.random.random_sample()

# Print the random value
print("Random Value:", random_value)

2. How to use numpy.random.random_sample() for random sampling:

Learn how to use NumPy's random_sample() function for generating random values.

import numpy as np

# Generate a random value between 0 and 1
random_value = np.random.random_sample()

# Print the random value
print("Random Value:", random_value)

3. Numpy random_sample() function for random value generation:

Explore the usage of NumPy's random_sample() 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_sample()

# Print the random value
print("Random Value:", random_value)

4. Python numpy.random_sample() usage for random sampling:

Learn how to use the numpy.random_sample() function in Python for random value sampling.

import numpy as np

# Generate a random value between 0 and 1
random_value = np.random.random_sample()

# Print the random value
print("Random Value:", random_value)

5. Sample code for random value sampling in NumPy:

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_sample(size=5)

# Print the random values
print("Random Values:", random_values)

6. Generating random values with NumPy in Python:

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_sample(size=3)

# Print the random values
print("Random Values:", random_values)

7. Numpy random_sample vs random.uniform for random sampling:

Compare NumPy's random_sample with random.uniform for random value sampling.

import numpy as np
import random

# Using NumPy random_sample
random_values_np = np.random.random_sample(size=5)

# Using random.uniform
random_values_uniform = [random.uniform(0, 1) for _ in range(5)]

# Print the random values
print("Random Values (NumPy):", random_values_np)
print("Random Values (uniform):", random_values_uniform)

8. Python numpy.random.random_sample() for array randomization:

Utilize numpy.random.random_sample() 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_sample
randomized_array = np.random.random_sample(size=len(original_array))
np.random.shuffle(randomized_array)

# Print the randomized array
print("Original Array:", original_array)
print("Randomized Array:", randomized_array)