Python Generate random number using numpy library

Python: Generate Random numbers: Here, we will figure out how to produce arbitrary numbers utilizing numpy library in a python programming language?

Objective: To hypothesize and produce irregular numbers utilizing numpy library

Arbitrary Number Generation: Random number age insignificant in the field of AI. It is utilized to instate loads in pretty much every AI calculation.

So not many capacities utilized for AI calculations from numpy library:

  1. numpy.random.rand() It comes to fruition of the exhibit as its contention and afterwards produce arbitrary numbers and fill entirely the cluster with the irregular numbers that lie in the middle of 0 and 1. The dispersion of arbitrary numbers pursues uniform appropriation.
  2. numpy.random.randint(): It takes two arguments(low and high). It creates an arbitrary number among low and high in which low is comprehensive and high is elite. It pursues discrete uniform circulation.
  3. numpy.random.randn(): It comes to fruition of the cluster as its contention and produces arbitrary numbers as gaussian circulation with mean as 0 and difference as 1. It observes standard ordinary appropriation.
  4. numpy.random.random(): It accepts size as its contention and produces irregular number arbitrary number lying somewhere in the range of 0 and 1. It pursues consistent arbitrary conveyance.
  5. numpy.random.multivariate(): It basically takes three arguments(mean of an individual element in the type of matrix, co – difference network and last contention is a number of data points). For creating information for more than one element, mean and difference network must be of higher measurement. It pursues multivariate ordinary circulation.

Python usage:

import numpy as np

print("###########random.rand()############")
A = np.random.rand(2,5)
print(A)
print("###########random.randint()############")
B = np.random.randint(2,17)
print(B)
print("###########random.randn()############")
C = np.random.randn(2,5)
print(C)
print("###########random.random()############")
D = np.random.random((2,5))
print(D)
print("###########random.multivariate_normal()############")
E = np.random.multivariate_normal([1.0,5.0], [[1.0,2.0],[2.0,1.0]],5)
print(E)

Output:

###########random.rand()############
[[0.87736653 0.75351615 0.06455974 0.36414861 0.04139118]
 [0.41138255 0.10342316 0.05800631 0.12752116 0.33958441]]
###########random.randint()############
12
###########random.randn()############
[[ 0.01895673  0.50055148  0.12352832 -0.35232071  0.03695278]
 [ 2.02632408  0.94237563  0.60807025 -0.37935715  1.45447358]]
###########random.random()############
[[0.57192619 0.85141271 0.49857667 0.62128599 0.39234191]
 [0.72266235 0.05779006 0.99732815 0.27651905 0.14774923]]
###########random.multivariate_normal()############
/home/main.py:16: RuntimeWarning: covariance is not positive-semidefinite.
  E = np.random.multivariate_normal([1.0,5.0], [[1.0,2.0],[2.0,1.0]],5)
[[ 2.27370346  4.71914942]
 [-0.222617    4.50092221]
 [-0.38584754  4.88753041]
 [ 2.2530275   5.5017934 ]
 [-0.13875541  3.25742664]]
 

Leave a Comment

error: Alert: Content is protected!!