Home » numpy random rand in python

numpy random rand in python

Spring Framework Basics Video Course
Java SE 11 Programmer I [1Z0-815] Practice Tests
Oracle Java Certification
1 Year Subscription
Java SE 11 Programmer II [1Z0-816] Practice Tests
Java SE 11 Developer (Upgrade) [1Z0-817]

The numpy random rand function generates the uniform distributed random numbers and creates an array of the given shape.

Syntax

random.rand(d0, d1, , dn)

Random values in a given shape.

Note

This is a convenience function for users porting code from Matlab, and wraps random_sample. That function takes a tuple to specify the size of the output, which is consistent with other NumPy functions like numpy.zeros and numpy.ones.

Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1).

Parameters
d0, d1, …, dnint, optional
The dimensions of the returned array, must be non-negative. If no argument is given a single Python float is returned.
Returns
outndarray, shape (d0, d1, ..., dn)

Examples

Create 1D Array

import numpy as np

arr1D = np.random.rand(5)

print(arr1D)

I saw this

>>> %Run numprandomrand.py
[0.43668982 0.26615951 0.47310883 0.13066627 0.97557206]

Create 2D Array

import numpy as np

arr2D = np.random.rand(3, 2)

print(arr2D)

I saw this

>>> %Run numprandomrand.py
[[0.54167831 0.52385086]
 [0.35677069 0.85545945]
 [0.83590527 0.73906704]]

Create 3D Array

import numpy as np

arr3D = np.random.rand(3, 2, 4)

print(arr3D)

I saw this

>>> %Run numprandomrand.py
[[[0.13450054 0.62685301 0.09636076 0.25121155]
  [0.58905256 0.60778877 0.86005363 0.32883141]]

 [[0.32508184 0.18744661 0.26799856 0.38133114]
  [0.12255183 0.92855853 0.13053893 0.72069251]]

 [[0.37820463 0.57092734 0.04135081 0.47663122]
  [0.4330011  0.11432421 0.7676993  0.9569132 ]]]

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More