Numpy is the best Python package for creating the array and doing complex mathematical calculations. There are many functions provided by the numpy. The numpy random is one of them. In this post, you will learn how to implement the Numpy Random function in Python.
Syntax of the numpy random function
Before going to the steps part lets know the numpy random function.
numpy.random.rand(d0, d1, ..., dn)
Steps to Implement Numpy Random Function in Python
Let me know all the steps to implement the numpy random function.
Step 1: Install NumPy
The first step is to install the Numpy in your system. If it’s already installed in your system then move to the second step. If you haven’t already installed NumPy, you can do so using the pip command
pip install numpy # For python 2.xx
pip3 install numpy # For python 3.xx
Step 2: Import NumPy
In your Python script or notebook, import the NumPy library using the import statement.
import numpy as np
Step 3: Generate Random Numbers
Now after importing the numpy module lets use the NumPy’s random module to generate random numbers. Below are the examples.
Uniform Distribution
# Generate a random float between 0 and 1
random_float = np.random.rand()
# Generate an array of random floats between 0 and 1
random_array = np.random.rand(5)
Normal (Gaussian) Distribution
# Generate a random float from a normal distribution
random_normal = np.random.randn()
# Generate an array of random floats from a normal distribution
random_normal_array = np.random.randn(5)
Generate Random Integer Numbers
# Generate a random integer between a specified range
random_int = np.random.randint(1, 10) # Generates between 1 and 9
# Generate an array of random integers between a specified range
random_int_array = np.random.randint(1, 10, size=5)
Random Permutations
# Shuffle an array
original_array = np.array([1, 2, 3, 4, 5])
shuffled_array = np.random.permutation(original_array)
Step 4: Use seed for random number generation
If you want reproducible results, you can set a seed using np.random.seed()
before generating random numbers. This ensures that the same sequence of random numbers is generated each time you run the code.
np.random.seed(42) # Use any integer as the seed value
This is particularly useful when you want others to reproduce your results. It means they want the same random number generated in their code.
Below is the full code for the entire steps.
import numpy as np
# Set seed for reproducibility
np.random.seed(42)
# Generate random numbers
random_float = np.random.rand()
random_array = np.random.rand(5)
random_normal = np.random.randn()
random_normal_array = np.random.randn(5)
random_int = np.random.randint(1, 10)
random_int_array = np.random.randint(1, 10, size=5)
# Shuffle an array
original_array = np.array([1, 2, 3, 4, 5])
shuffled_array = np.random.permutation(original_array)
Conclusion
The numpy.random function is a very useful function if you want to quickly generate numbers to implement some logic in your code. The above are the steps to implement numpy.random function in Python.
import numpy as np
# Set seed for reproducibility
np.random.seed(42)
# Generate random numbers
random_float = np.random.rand()
random_array = np.random.rand(5)
random_normal = np.random.randn()
random_normal_array = np.random.randn(5)
random_int = np.random.randint(1, 10)
random_int_array = np.random.randint(1, 10, size=5)
# Shuffle an array
original_array = np.array([1, 2, 3, 4, 5])
shuffled_array = np.random.permutation(original_array)
These are the basic steps to implement NumPy random functions in Python. You can explore more options and fine-tune parameters based on your specific needs.
Leave a Reply