Python:NumPy Random Module
In NumPy, the random module is used for generating random numbers, sampling, and performing statistical simulations. It provides a suite of functions to generate random values, including integers, floating-point numbers, and samples from various probability distributions.
Unlike Python’s built-in random module, NumPy’s random module is optimized for performance and can generate random numbers more efficiently, especially for large arrays.
Some of the common functions that the random module in NumPy provides include:
.rand().randint().choice().normal().uniform().binomial()
Applications
The random module in NumPy is widely used in various fields, including:
- Machine Learning and AI: Initializing weights in neural networks, data augmentation, and Monte Carlo simulations.
- Statistical Simulations: Running probability-based simulations for hypothesis testing.
- Game Development: Generating random movements or events in games.
- Data Science and Analysis: Random sampling, bootstrapping, and resampling datasets.
- Cryptography and Security: Generating random keys, tokens, and nonces.
Syntax
The general syntax for the functions under the random module in NumPy is following:
np.random.func(params)
func: The specific function from the NumPy random module (e.g.,.rand(),.randint(),.choice()).params: The parameters to be used in the function.
Example
The following example demonstrates the usage of some common functions under the random module in NumPy:
import numpy as np# Generate a random float in the range 0-1random_float = np.random.rand()print("Random Float:", random_float)# Generate a random integer between 10 and 50random_int = np.random.randint(10, 50)print("Random Integer:", random_int)# Select 3 random elements from a listsample_list = [10, 20, 30, 40, 50]random_sample = np.random.choice(sample_list, 3)print("Random Sample:", random_sample)# Generate 3 random numbers from a normal distribution with mean '0' and standard deviation '1'random_normal = np.random.normal(0, 1, 3)print("Random Normal Distribution:", random_normal)# Generate 3 random numbers from a uniform distribution between 5 and 15random_uniform = np.random.uniform(5, 15, 3)print("Random Uniform Distribution:", random_uniform)# Generate 5 binomially distributed numbers with 10 trials and 0.5 probabilityrandom_binomial = np.random.binomial(10, 0.5, 5)print("Random Binomial Distribution:", random_binomial)
The above code produces the following output:
Random Float: 0.002883955111148917Random Integer: 13Random Sample: [30 40 30]Random Normal Distribution: [ 1.42832105 0.54950071 -0.46675773]Random Uniform Distribution: [ 7.15527759 9.39970904 10.51563253]Random Binomial Distribution: [4 4 5 4 5]
Note: The output values will change each time the code is executed because the functions generate random numbers, meaning the results are not fixed and will vary on each run.
Random Module
- .beta()
- Generates random values based on a beta distribution.
- .binomial()
- Draws random samples from a binomial distribution.
- .chisquare()
- Generates random values based on a chi-square distribution with a given number of degrees of freedom.
- .choice()
- Generates a random selection of elements from an array with or without replacement.
- .exponential()
- Generates random samples from an exponential distribution using a specified scale.
- .gamma()
- Generates random samples from a Gamma distribution.
- .geometric()
- Generates random samples from the specified geometric distribution.
- .normal()
- Generates random samples from a normal (Gaussian) distribution.
- .poisson()
- Returns random integers from a Poisson distribution.
- .power()
- Returns a NumPy array of random samples drawn from the power distribution.
- .rand()
- Returns an array of the provided shape filled with randomly generated samples from a uniform distribution over [0, 1).
- .randn()
- Generates an array containing random numbers from the standard normal distribution.
- .shuffle()
- Randomly rearranges the elements of an array in-place along the first axis, modifying the original array.
- randint()
- Generates random integers within a specified range using NumPy.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours