Tutorial Gateway

  • C
  • C#
  • Python
  • SQL
  • Java
  • JS
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs
  • MySQL

Python random Array

In Python, we have the random module used to generate random numbers of a given type using the PRNG algorithm. Here, we are going to discuss the list of available functions to generate a random array in Python.

Python random Array using rand

The Numpy random rand function creates an array of random numbers from 0 to 1. Here, you have to specify the shape of an array.

import numpy as np
 
arr = np.random.rand(7)
 
print('-----Generated Random Array----')
print(arr)
 
arr2 = np.random.rand(10)
print('\n-----Generated Random Array----')
print(arr2)
Python random Array using rand

Python 2D Random Array

Here, we are using this random rand function to generate a random two-dimensional array.

import numpy as np
 
arr = np.random.rand(2, 2)
 
print('-----Generated Two Dimensional Random Array----')
print(arr)
 
arr2 = np.random.rand(4, 5)
print('\n-----Generated Two Dimensional Random Array----')
print(arr2)
Python Two Dimensional Random Array using rand

Python 3D Random Array

Let us create a random three-dimensional array using random rand function.

import numpy as np
 
arr = np.random.rand(2, 2, 2)
 
print('-----Generated Three Dimensional Random Array----')
print(arr)
 
arr2 = np.random.rand(2, 4, 5)
print('\n-----Generated Three Dimensional Random Array----')
print(arr2)
Python Three Dimensional Random Array using rand

Python random randn

The randn function generates random arrays of one, 2D, and 3D Arrays.

import numpy as np
 
arr = np.random.randn(5)
 
print('-----Generated Random Array----')
print(arr)
 
arr2 = np.random.randn(8)
print('\n-----Generated Random Array----')
print(arr2)
Python random randn

This time we are generating two dimensional and three-dimensional random arrays using this Numpy random randn function.

import numpy as np
 
arr = np.random.randn(5, 4)
 
print('-----Generated Two Dimensional Random Array----')
print(arr)
 
arr2 = np.random.randn(2, 3, 5)
print('\n-----Generated Three Dimensional Random Array----')
print(arr2)
Python random array using randn 1

Python random Array using random function

In this example, we are using the Numpy random function available in the random module to generate an array of random numbers of length 6 and 8

import numpy as np
 
arr = np.random.random(5)
 
print('-----Generated Random Array----')
print(arr)
 
arr2 = np.random.random((4, 5)) # Array size 4 * 5
print('\n-----Generated Two Dimensional Random Array----')
print(arr2)
 
arr3 = np.random.random((2, 3, 5)) # Array size 2 * 3 * 5
print('\n-----Generated Three Dimensional Random Array----')
print(arr3)
Python random Array using random function

Python random randint

The Numpy random randint function returns an integer array from low value to high value of given size. The syntax of this Numpy function in Python is.

numpy.random.randint(low, high = None, size = None, type = ‘l’)

Let us see an example

import numpy as np
 
arr = np.random.randint(0, 5, size = 4)
 
print('-----Generated Random Array----')
print(arr)
 
arr2 = np.random.randint(10, 50, size = (5, 10))
print('\n-----Generated Two Dimensional Random Array----')
print(arr2)
 
arr3 = np.random.randint(1, 40, size = (2, 3, 7))
print('\n-----Generated Three Dimensional Random Array----')
print(arr3)
Python random randint array

Python random normal

The Python random normal function generates random numbers from a normal distribution. This Numpy normal accepts the size of an array then fills that array with normally distributed values.

import numpy as np
 
arr = np.random.normal(size = 4)
 
print('-----Generated Random Array----')
print(arr)
 
arr2 = np.random.normal(size = (5, 5))
print('\n-----Generated Two Dimensional Random Array----')
print(arr2)
 
arr3 = np.random.normal(size = (2, 3, 4))
print('\n-----Generated Three Dimensional Random Array----')
print(arr3)
Python random normal array

Python random uniform

The Python random uniform function generates a uniform distribution of random numbers. This random uniform accepts the array size and fills that array with uniform distributed values.

import numpy as np
 
arr = np.random.uniform(size = 5)
 
print('-----Generated Random Array----')
print(arr)
 
arr2 = np.random.uniform(size = (5, 5))
print('\n-----Generated Two Dimensional Random Array----')
print(arr2)
 
arr3 = np.random.uniform(size = (2, 3, 4))
print('\n-----Generated Three Dimensional Random Array----')
print(arr3)
Python random uniform array

Filed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda Function
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File Handling
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python numpy concatenate
  • Python numpy Array shape
  • Python pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python substring
  • Python Programming Examples

Copyright © 2021· All Rights Reserved by Suresh.
About | Contact | Privacy Policy