
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
Numpy random.randn() Function
The Numpy random.randn() function used to generate a numpy array of specified shape and fills it with random values as per standard normal distribution.
If positive arguments are provided, randn generates an array of shape (d0, d1, , dn), filled with random floats sampled from a univariate normal (Gaussian) distribution of mean 0 and variance 1. A single float randomly sampled from the distribution is returned if no argument is provided.
Syntax
Following is the syntax of the Numpy random.randn() function −
numpy.random.randn(d0, d1, ..., dn)
Parameters
Following are the parameters of the Numpy random.randn() function −
- d0, d1, ..., dn - These are the dimensions of the output array.
Return Values
This function returns a NumPy array filled with random floats following a standard normal distribution.
Example
Following is a basic example to generate a single random float using Numpy random.randn() function −
import numpy as np random_value = np.random.randn() print("Random Value -", random_value) print(type(random_value))
Output
Following is the output of the above code −
Random Value - 0.3946743245523318 <class 'float'>
Example : Numpy array using 'random.randn()'
The numpy.random.randn() function can also be used to generate a 1D NumPy array of random values from a standard normal distribution.
In the following example, a 1D array of 4 elements is generated using numpy.random.randn() function −
import numpy as np numpy_array = np.random.randn(4) print("1D Array of Random Values - \n", numpy_array) print(type(numpy_array))
Output
Following is the output of the above code:
1D Array of Random Values - [ 1.30524442 -0.74706264 0.53150742 -1.29810795] <class 'numpy.ndarray'>
Example : Multi-dimensional Numpy Array
Using the numpy.random.randn() function, we can generate n-dimensional arrays of random values by specifying the desired dimensions as parameters.
In this example, a 3D array with dimensions 3x4x3 is been generated, filled with random values from the standard normal distribution −
import numpy as np numpy_3d_array = np.random.randn(3, 4, 3) print("3D Array of Random Values -\n", numpy_3d_array)
Output
Following is the output of the above code −
3D Array of Random Values - [[[ 0.62852014 1.41807874 0.83575845] [ 1.00913888 -0.62230585 0.67617611] [-0.51083778 0.52039257 -0.84454698] [ 1.22446679 -0.13410954 -0.33386194]] [[ 0.41440124 1.00212288 0.39328479] [ 0.51793246 1.03868843 1.23749478] [-2.23742862 0.0430593 -0.60495951] [ 0.0221033 0.75218868 -0.02696248]] [[ 0.4918343 0.72076167 -1.40222382] [ 2.43693037 -0.83250305 0.26874105] [-0.54347424 2.25655853 0.45657885] [-1.01706963 0.6131971 0.45100215]]]
Example : Passing Negative Arguments
When we pass a negative argument to the numpy.random.randn() function, it raises a ValueError.
In the following example, we have passed a -6(negative) number to the numpy.random.randn() function −
#importing numpy array import numpy as np #passing a negative integers my_Array = np.random.randn(-6) print("Numpy Array",my_Array)
Output
Following is the output of the above code −
Traceback (most recent call last): File "/home/cg/root/52187/main.py", line 4, in <module> my_Array = np.random.randn(-6) File "mtrand.pyx", line 1270, in numpy.random.mtrand.RandomState.randn File "mtrand.pyx", line 1431, in numpy.random.mtrand.RandomState.standard_normal File "_common.pyx", line 636, in numpy.random._common.cont ValueError: negative dimensions are not allowed