Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs
    • SQL FAQ’s

Python random Array

by suresh

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)

OUTPUT

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)

OUTPUT

Python Two Dimensional Random Array using rand

Python 3D Random Array

Let us create a random three-dimensional Python 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)

OUTPUT

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)

OUTPUT

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)

OUTPUT

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)

OUTPUT

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 os.

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)

OUTPUT

Python random randint array

Python random normal

The Python random normal function generates random numbers from a normal distribution. This Python 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)

OUTPUT

Python random normal array

Python random uniform

The Python random uniform function generates a uniform distribution of random numbers. This Python 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)

OUTPUT

Python random uniform array

Placed 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
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File
  • 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
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy