In this Numpy tutorial we are going to talk about Numpy Random Number Generation in Python, so random numbers are used in different type of applications like generating synthetic datasets, testing algorithms and assessing statistical models, Numpy provides different functions for creating random numbers, in this article we want to talk about NumPy Random functions, NumPy random functions are capable of generating reproducible random numbers, making it easier to share and reproduce results.
Why NumPy Random ?
NumPy random module offers several advantages over Python built-in random module. It provides more comprehensive set of functions and distributions, better performance and the ability to generate arrays of random numbers efficiently.
Numpy Random Number Generation
NumPy random module offers different functions for generating random numbers. Let’s explore some of the commonly used ones:
Numpy rand() and random():
The rand() and random() functions generates random numbers uniformly distributed between 0 and 1. They are incredibly useful for generating random samples from a uniform distribution.
1 2 3 4 5 6 7 8 9 |
import numpy as np # Generate single random number rand_num = np.random.rand() print(rand_num) # Generate an array of random numbers rand_array = np.random.rand(3, 3) print(rand_array) |
This will be the result
Numpy randn():
The randn() function generates random numbers from a standard normal distribution (mean 0, standard deviation 1). This distribution is often used in statistical modeling and simulations.
1 2 3 4 5 6 7 8 9 |
import numpy as np # Generate a single random number rand_num = np.random.randn() print(rand_num) # Generate an array of random numbers rand_array = np.random.randn(3, 3) print(rand_array) |
Numpy randint():
The randint() function generates random integers from a specified low (inclusive) to high (exclusive) range. It is useful for simulating dice rolls, selecting random indices or creating synthetic discrete datasets.
1 2 3 4 5 6 7 8 9 10 11 |
import numpy as np # Generate a single random integer # between 1 and 6 (inclusive) rand_int = np.random.randint(1, 7) print(rand_int) # Generate a 1D array of random integers # between 1 and 100 (exclusive) rand_array = np.random.randint(1, 100, size=10) print(rand_array) |
This will be the result
Numpy Distributions:
NumPy random module offers different probability distributions, including the normal distribution (normal()), exponential distribution (exponential()), uniform distribution (uniform()) and more. These functions allows you to generate random numbers from specific distributions and control parameters such as mean, standard deviation and range.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import numpy as np # Generate random numbers from normal distribution rand_normal = np.random.normal(loc=0, scale=1, size=10) print(rand_normal) # Generate random numbers from an exponential distribution rand_exponential = np.random.exponential(scale=1, size=10) print(rand_exponential) # Generate random numbers from a uniform distribution rand_uniform = np.random.uniform(low=0, high=1, size=10) print(rand_uniform) |
Learn More on Python Numpy
- Python Numpy for Machine Learning
- How to Install Numpy
- Working with Linear Algebra in Numpy
- Numpy vs Pandas
- Advance Python Numpy Techniques
- Numpy Array Indexing and Slicing
- Numpy Mathematical Functions
Subscribe and Get Free Video Courses & Articles in your Email