Python numpy random rand

The Python numpy random rand function generates the uniform distributed random numbers and creates an array of the given shape. To work with this function, we have to import the NumPy module. The syntax of this Python numpy random rand function is

numpy.random.rand(d0, d1, d2,…., dn)

d0, d1, d2,…., dn values are optional, and they specify the array dimensions. For instance, if we pass 3 as the argument (3) means a one-dimensional array of size three with random samples.

Python numpy random rand Examples

As we said earlier, the rand function parameter values are optional, so we use this function without any parameter.

import numpy as tg

a = tg.random.rand()


Create 1D and 2D Array

This method creates an array of a given dimension. In the first set arr1D, we used the rand function to generate a one-dimensional random array of size eight. In this second statement, Arr2D, it returns the two-dimensional of three rows and five columns.

import numpy as tg

arr1D = tg.random.rand(8)


Arr2D = tg.random.rand(3, 5)

[0.51529486 0.70732475 0.13123041 0.08868923 0.91606594 0.94329656
 0.73875145 0.85248642]

[[0.82700324 0.78752106 0.91874764 0.71277277 0.41604479]
 [0.7745265  0.88292852 0.56248594 0.09944867 0.5522274 ]
 [0.70205439 0.90095325 0.20693606 0.74183462 0.38383534]]

Python numpy random rand 3D Array

import numpy as tg

randArr = tg.random.rand(3, 5, 6)

Python numpy random rand Example 1

Numpy ND or Multi-Dimensional random Array

import numpy as tg

a = tg.random.rand(2, 2, 2, 3, 4)


Python output

[[[[[2.22991745e-01 4.82875578e-01 4.02098798e-01 9.82463072e-01]
    [1.27502476e-02 3.78317995e-01 1.31346574e-01 3.58953956e-01]
    [6.31334854e-01 1.80433343e-02 7.73429470e-01 8.47566829e-01]]

   [[7.46991797e-01 9.49487163e-01 6.74764732e-01 8.73915802e-01]
    [1.66405826e-01 6.96463970e-01 2.18829056e-01 6.75685704e-01]
    [1.46913928e-02 2.66258908e-01 3.49072489e-02 7.51997693e-01]]]

  [[[3.15566931e-01 6.77939362e-01 5.68240002e-02 3.78304965e-02]
    [6.26829456e-01 1.64778430e-01 5.80110973e-01 9.45645604e-01]
    [4.52814994e-01 6.12752904e-02 3.98656682e-01 6.12022902e-01]]

   [[8.48650860e-01 9.58124465e-01 6.36882147e-02 5.82332018e-01]
    [4.78369858e-02 5.63250308e-01 6.18235746e-01 4.06755385e-01]
    [9.24566606e-02 8.76239936e-01 2.94192746e-02 1.79601830e-01]]]]

 [[[[2.31480056e-01 8.54221882e-01 6.02771565e-02 1.31022006e-02]
    [6.22401057e-03 3.08509460e-01 3.83590441e-01 8.70963661e-01]
    [9.10762417e-01 4.53411875e-01 5.39746576e-01 8.12068034e-01]]

   [[4.88355065e-01 9.90180989e-01 1.70643645e-01 9.42585049e-01]
    [5.10431032e-01 6.86724036e-01 8.80602910e-01 9.28994548e-01]
    [6.96880899e-04 8.14746646e-01 9.55883374e-02 2.45433858e-01]]]

  [[[9.05425064e-02 3.76958137e-01 4.04775524e-01 6.72941808e-01]
    [5.44979157e-01 4.18175802e-01 5.35779225e-01 1.90175407e-02]
    [2.70055421e-01 6.20119362e-01 6.02102212e-01 5.89668883e-01]]

   [[5.29601610e-01 8.34906099e-01 1.71963671e-01 7.76704873e-01]
    [4.85148164e-02 3.33561971e-01 9.33291412e-01 2.81507135e-01]
    [9.14183251e-01 1.89159336e-02 1.11227617e-01 2.97916564e-01]]]]]