What Is NumPy Random?
The NumPy random module is used to generate random numbers and random data arrays efficiently. It is widely used in data science, machine learning, simulations, statistical analysis, and testing.
Unlike Python’s built-in random module, NumPy’s random functions are faster, support vectorized operations, and can generate large multi-dimensional arrays of random values.
The NumPy random module is used to generate random numbers and random data arrays efficiently. It is widely used in data science, machine learning, simulations, statistical analysis, and testing.
Unlike Python’s built-in random module, NumPy’s random functions are faster, support vectorized operations, and can generate large multi-dimensional arrays of random values.
Why Use NumPy Random?
NumPy random is preferred when:
Working with large datasets
Generating random arrays or matrices
Performing statistical simulations
Training machine learning models
Ensuring reproducibility using random seeds
NumPy random is preferred when:
Working with large datasets
Generating random arrays or matrices
Performing statistical simulations
Training machine learning models
Ensuring reproducibility using random seeds
Importing NumPy Random
All random functions are accessed using np.random.
All random functions are accessed using np.random.
Generate Random Floats
random()
Generates random floating-point numbers between 0.0 and 1.0.
Pythonx = np.random.random() print(x) # Output: # 0.3745401188473625
Generates random floating-point numbers between 0.0 and 1.0.
x = np.random.random() print(x) # Output: # 0.3745401188473625Generate Random Integers
randint()
Generates random integers within a given range.
Pythonx = np.random.randint(1, 10) print(x) # Output: # 7
Lower bound is inclusive
Upper bound is exclusive
Generates random integers within a given range.
x = np.random.randint(1, 10) print(x) # Output: # 7Lower bound is inclusive
Upper bound is exclusive
Generate Random Arrays
Random 1D Array
Pythonarr = np.random.random(5) print(arr) # Output: # [0.12 0.87 0.45 0.66 0.31]
arr = np.random.random(5) print(arr) # Output: # [0.12 0.87 0.45 0.66 0.31]Random 2D Array
Pythonarr = np.random.random((2, 3)) print(arr) # Output: # [[0.41 0.72 0.18] # [0.55 0.09 0.63]]
arr = np.random.random((2, 3)) print(arr) # Output: # [[0.41 0.72 0.18] # [0.55 0.09 0.63]]rand() – Random Values Between 0 and 1
Pythonarr = np.random.rand(3, 2) print(arr) # Output: # [[0.54 0.12] # [0.89 0.77] # [0.33 0.66]]
Accepts dimensions as arguments
Values are floats between 0 and 1
arr = np.random.rand(3, 2) print(arr) # Output: # [[0.54 0.12] # [0.89 0.77] # [0.33 0.66]]Accepts dimensions as arguments
Values are floats between
0and1
randn() – Random Values from Standard Normal Distribution
Generates values following a normal distribution (mean = 0, standard deviation = 1).
Pythonarr = np.random.randn(4) print(arr) # Output: # [ 0.45 -1.23 0.67 0.09 ]
Used heavily in statistics and machine learning.
Generates values following a normal distribution (mean = 0, standard deviation = 1).
arr = np.random.randn(4) print(arr) # Output: # [ 0.45 -1.23 0.67 0.09 ]Used heavily in statistics and machine learning.
Random Choice from an Array
choice()
Selects random elements from a given array.
Pythondata = [10, 20, 30, 40] x = np.random.choice(data) print(x) # Output: # 30
Selects random elements from a given array.
data = [10, 20, 30, 40] x = np.random.choice(data) print(x) # Output: # 30Multiple Random Choices
Pythonx = np.random.choice(data, size=3) print(x) # Output: # [20 40 10]
x = np.random.choice(data, size=3) print(x) # Output: # [20 40 10] Shuffle Data
shuffle()
Randomly rearranges elements in place.
Pythonarr = np.array([1, 2, 3, 4, 5]) np.random.shuffle(arr) print(arr) # Output: # [3 5 1 2 4]
Randomly rearranges elements in place.
arr = np.array([1, 2, 3, 4, 5]) np.random.shuffle(arr) print(arr) # Output: # [3 5 1 2 4] Set Random Seed (Reproducibility)
Using a seed ensures the same random output every time the program runs.
Pythonnp.random.seed(42) print(np.random.random()) # Output: # 0.3745401188473625
This is critical for debugging, testing, and ML experiments.
Using a seed ensures the same random output every time the program runs.
np.random.seed(42) print(np.random.random()) # Output: # 0.3745401188473625 This is critical for debugging, testing, and ML experiments.
Common NumPy Random Functions
Function Purpose random()Random float (0 to 1) randint()Random integers rand()Random floats (array) randn()Normal distribution choice()Random selection shuffle()Shuffle array seed()Set reproducible results
| Function | Purpose |
|---|---|
random() | Random float (0 to 1) |
randint() | Random integers |
rand() | Random floats (array) |
randn() | Normal distribution |
choice() | Random selection |
shuffle() | Shuffle array |
seed() | Set reproducible results |