The random is a module present in the NumPy library. This module contains the functions which are used for generating random numbers. This module contains some simple random data generation methods, some permutation and distribution functions, and random generator functions.
All the functions in a random module are as follows:
There are the following functions of simple random data:
1) p.random.rand(d0, d1, ..., dn)
This function of random module is used to generate random numbers or values in a given shape.
Example:
import numpy as np a=np.random.rand(5,2) a
Output:
2) np.random.randn(d0, d1, ..., dn)
This function of random module return a sample from the "standard normal" distribution.
Example:
import numpy as np a=np.random.randn(2,2) a
Output:
3) np.random.randint(low[, high, size, dtype])
This function of random module is used to generate random integers from inclusive(low) to exclusive(high).
Example:
import numpy as np a=np.random.randint(3, size=10) a
Output:
4) np.random.random_integers(low[, high, size])
This function of random module is used to generate random integers number of type np.int between low and high.
Example:
import numpy as np a=np.random.random_integers(3) a b=type(np.random.random_integers(3)) b c=np.random.random_integers(5, size=(3,2)) c
Output:
5) np.random.random_sample([size])
This function of random module is used to generate random floats number in the half-open interval [0.0, 1.0).
Example:
import numpy as np a=np.random.random_sample() a b=type(np.random.random_sample()) b c=np.random.random_sample((5,)) c
Output:
6) np.random.random([size])
This function of random module is used to generate random floats number in the half-open interval [0.0, 1.0).
Example:
import numpy as np a=np.random.random() a b=type(np.random.random()) b c=np.random.random((5,)) c
Output:
7) np.random.ranf([size])
This function of random module is used to generate random floats number in the half-open interval [0.0, 1.0).
Example:
import numpy as np a=np.random.ranf() a b=type(np.random.ranf()) b c=np.random.ranf((5,)) c
Output:
8) np.random.sample([size])
This function of random module is used to generate random floats number in the half-open interval [0.0, 1.0).
Example:
import numpy as np a=np.random.sample() a b=type(np.random.sample()) b c=np.random.sample((5,)) c
Output:
9) np.random.choice(a[, size, replace, p])
This function of random module is used to generate random sample from a given 1-D array.
Example:
import numpy as np a=np.random.choice(5,3) a b=np.random.choice(5,3, p=[0.2, 0.1, 0.4, 0.2, 0.1]) b
Output:
10) np.random.bytes(length)
This function of random module is used to generate random bytes.
Example:
import numpy as np a=np.random.bytes(7) a
Output:
There are the following functions of permutations:
1) np.random.shuffle()
This function is used for modifying a sequence in-place by shuffling its contents.
Example:
import numpy as np a=np.arange(12) a np.random.shuffle(a) a
Output:
2) np.random.permutation()
This function permute a sequence randomly or return a permuted range.
Example:
import numpy as np a=np.random.permutation(12) a
Output:
There are the following functions of permutations:
1) beta(a, b[, size])
This function is used to draw samples from a Beta distribution.
Example:
def setup(self): self.dist = dist.beta self.cargs = [] self.ckwd = dict(alpha=2, beta=3) self.np_rand_fxn = numpy.random.beta self.np_args = [2, 3] self.np_kwds = dict()
2) binomial(n, p[, size])
This function is used to draw sample from a binomial distribution.
Example:
import numpy as np n, p = 10, .6 s1= np.random.binomial(n, p, 10) s1
Output:
3) chisquare(df[, size])
This function is used to draw sample from a binomial distribution.
Example:
import numpy as np np.random.chisquare(2,4) sum(np.random.binomial(9, 0.1, 20000) == 0)/20000.
Output:
4) dirichlet(alpha[, size])
This function is used to draw a sample from the Dirichlet distribution.
Example:
Import numpy as np import matplotlib.pyplot as plt s1 = np.random.dirichlet((10, 5, 3), 20).transpose() plt.barh(range(20), s1[0]) plt.barh(range(20), s1[1], left=s1[0], color='g') plt.barh(range(20), s1[2], left=s1[0]+s1[1], color='r') plt.title("Lengths of Strings") plt.show()
Output:
5) exponential([scale, size])
This function is used to draw sample from an exponential distribution.
Example:
def __init__(self, sourceid, targetid): self.__type = 'Transaction' self.id = uuid4() self.source = sourceid self.target = targetid self.date = self._datetime.date(start=2015, end=2019) self.time = self._datetime.time() if random() < 0.05: self.amount = self._numbers.between(100000, 1000000) self.amount = npr.exponential(10) if random() < 0.15: self.currency = self._business.currency_iso_code() else: self.currency = None
6) f(dfnum, dfden[, size])
This function is used to draw sample from an F distribution.
Example:
import numpy as np dfno= 1. dfden = 48. s1 = np.random.f(dfno, dfden, 10) np.sort(s1)
Output:
7) gamma(shape[, scale, size])
This function is used to draw sample from a Gamma distribution
Example:
import numpy as np shape, scale = 2., 2. s1 = np.random.gamma(shape, scale, 1000) import matplotlib.pyplot as plt import scipy.special as spss count, bins, ignored = plt.hist(s1, 50, density=True) a = bins**(shape-1)*(np.exp(-bins/scale) / (spss.gamma(shape)*scale**shape)) plt.plot(bins, a, linewidth=2, color='r') plt.show()
8) geometric(p[, size])
This function is used to draw sample from a geometric distribution.
Example:
import numpy as np a = np.random.geometric(p=0.35, size=10000) (a == 1).sum() / 1000
Output:
9) gumbel([loc, scale, size])
This function is used to draw sample from a Gumble distribution.
Example:
import numpy as np lov, scale = 0, 0.2 s1 = np.random.gumbel(loc, scale, 1000) import matplotlib.pyplot as plt count, bins, ignored = plt.hist(s1, 30, density=True) plt.plot(bins, (1/beta)*np.exp(-(bins - loc)/beta)* np.exp( -np.exp( -(bins - loc) /beta) ),linewidth=2, color='r') plt.show()
Output:
10) hypergeometric(ngood, nbad, nsample[, size])
This function is used to draw sample from a Hypergeometric distribution.
Example:
import numpy as np good, bad, samp = 100, 2, 10 s1 = np.random.hypergeometric(good, bad, samp, 1000) plt.hist(s1) plt.show()
Output:
11) laplace([loc, scale, size])
This function is used to draw sample from the Laplace or double exponential distribution with specified location and scale.
Example:
import numpy as np location, scale = 0., 2. s = np.random.laplace(location, scale, 10) s
Output:
12) logistic([loc, scale, size])
This function is used to draw sample from logistic distribution.
Example:
import numpy as np import matplotlib.pyplot as plt location, scale = 10, 1 s1 = np.random.logistic(location, scale, 10000) count, bins, ignored = plt.hist(s1, bins=50) count bins ignored plt.show()
Output:
13) lognormal([mean, sigma, size])
This function is used to draw sample from a log-normal distribution.
Example:
import numpy as np mu, sigma = 2., 1. s1 = np.random.lognormal(mu, sigma, 1000) import matplotlib.pyplot as plt count, bins, ignored = plt.hist(s1, 100, density=True, align='mid') a = np.linspace(min(bins), max(bins), 10000) pdf = (np.exp(-(np.log(a) - mu)**2 / (2 * sigma**2))/ (a * sigma * np.sqrt(2 * np.pi))) plt.plot(a, pdf, linewidth=2, color='r') plt.axis('tight') plt.show()
Output:
14) logseries(p[, size])
This function is used to draw sample from a logarithmic distribution.
Example:
import numpy as np x = .6 s1 = np.random.logseries(x, 10000) count, bins, ignored = plt.hist(s1) def logseries(k, p): return -p**k/(k*log(1-p)) plt.plot(bins, logseries(bins, x)*count.max()/logseries(bins, a).max(), 'r') plt.show()
Output:
15) multinomial(n, pvals[, size])
This function is used to draw sample from a multinomial distribution.
Example:
import numpy as np np.random.multinomial(20, [1/6.]*6, size=1)
Output:
16) multivariate_normal(mean, cov[, size, ...)
This function is used to draw sample from a multivariate normal distribution.
Example:
import numpy as np mean = (1, 2) coveriance = [[1, 0], [0, 100]] import matplotlib.pyplot as plt a, b = np.random.multivariate_normal(mean, coveriance, 5000).T plt.plot(a, b, 'x') plt.axis('equal'023 030 ) plt.show()
Output:
17) negative_binomial(n, p[, size])
This function is used to draw sample from a negative binomial distribution.
Example:
import numpy as np s1 = np.random.negative_binomial(1, 0.1, 100000) for i in range(1, 11): probability = sum(s1<i) / 100000. print i, "wells drilled, probability of one success =", probability
Output:
18) noncentral_chisquare(df, nonc[, size])
This function is used to draw sample from a noncentral chi-square distribution.
Example:
import numpy as np import matplotlib.pyplot as plt val = plt.hist(np.random.noncentral_chisquare(3, 25, 100000), bins=200, normed=True) plt.show()
Output:
19) normal([loc, scale, size])
This function is used to draw sample from a normal distribution.
Example:
import numpy as np import matplotlib.pyplot as plt mu, sigma = 0, 0.2 # mean and standard deviation s1 = np.random.normal(mu, sigma, 1000) abs(mu - np.mean(s1)) < 0.01 abs(sigma - np.std(s1, ddof=1)) < 0.01 count, bins, ignored = plt.hist(s1, 30, density=True) plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) *np.exp( - (bins - mu)**2 / (2 * sigma**2) ), linewidth=2, color='r') plt.show()
Output:
20) pareto(a[, size])
This function is used to draw samples from a Lomax or Pareto II with specified shape.
Example:
import numpy as np import matplotlib.pyplot as plt b, m1 = 3., 2. # shape and mode s1 = (np.random.pareto(b, 1000) + 1) * m1 count, bins, _ = plt.hist(s1, 100, density=True) fit = b*m**b / bins**(b+1) plt.plot(bins, max(count)*fit/max(fit), linewidth=2, color='r') plt.show()
Output:
21) power(a[, size])
This function is used to draw samples in [0, 1] from a power distribution with positive exponent a-1.
Example:
import numpy as np x = 5. # shape samples = 1000 s1 = np.random.power(x, samples) import matplotlib.pyplot as plt count, bins, ignored = plt.hist(s1, bins=30) a = np.linspace(0, 1, 100) b = x*a**(x-1.) density_b = samples*np.diff(bins)[0]*b plt.plot(a, density_b) plt.show()
Output:
22) rayleigh([scale, size])
This function is used to draw sample from a Rayleigh distribution.
Example:
val = hist(np.random.rayleigh(3, 100000), bins=200, density=True) meanval = 1 modeval = np.sqrt(2 / np.pi) * meanval s1 = np.random.rayleigh(modeval, 1000000) 100.*sum(s1>3)/1000000.
Output:
23) standard_cauchy([size])
This function is used to draw sample from a standard Cauchy distribution with mode=0.
Example:
import numpy as np import matplotlib.pyplot as plt s1 = np.random.standard_cauchy(1000000) s1 = s1[(s1>-25) & (s1<25)] # truncate distribution so it plots well plt.hist(s1, bins=100) plt.show()
Output:
24) standard_exponential([size])
This function is used to draw sample from a standard exponential distribution.
Example:
import numpy as np n = np.random.standard_exponential((2, 7000))
Output:
25) standard_gamma([size])
This function is used to draw sample from a standard Gamma distribution.
Example:
import numpy as np shape, scale = 2., 1. s1 = np.random.standard_gamma(shape, 1000000) import matplotlib.pyplot as plt import scipy.special as sps count1, bins1, ignored1 = plt.hist(s, 50, density=True) y = bins1**(shape-1) * ((np.exp(-bins1/scale))/ (sps.gamma(shape) * scale**shape)) plt.plot(bins1, y, linewidth=2, color='r') plt.show()
Output:
26) standard_normal([size])
This function is used to draw sample from a standard Normal distribution.
Example:
import numpy as np import matplotlib.pyplot as plt s1= np.random.standard_normal(8000) s1 q = np.random.standard_normal(size=(3, 4, 2)) q
Output:
27) standard_t(df[, size])
This function is used to draw sample from a standard Student's distribution with df degree of freedom.
Example:
intake = np.array([5260., 5470, 5640, 6180, 6390, 6515, 6805, 7515,8230,8770]) s1 = np.random.standard_t(10, size=100000) np.mean(intake) intake.std(ddof=1) t = (np.mean(intake)-7725)/(intake.std(ddof=1)/np.sqrt(len(intake))) h = plt.hist(s1, bins=100, density=True) np.sum(s1<t) / float(len(s1)) plt.show()
Output:
28) triangular(left, mode, right[, size])
This function is used to draw sample from a triangular distribution over the interval.
Example:
import numpy as np import matplotlib.pyplot as plt h = plt.hist(np.random.triangular(-4, 0, 8, 1000000), bins=300,density=True) plt.show()
Output:
29) uniform([low, high, size])
This function is used to draw sample from a uniform distribution.
Example:
import numpy as np import matplotlib.pyplot as plt s1 = np.random.uniform(-1,0,1000) np.all(s1 >= -1) np.all(s1 < 0) count, bins, ignored = plt.hist(s1, 15, density=True) plt.plot(bins, np.ones_like(bins), linewidth=2, color='r') plt.show()
Output:
30) vonmises(m1, m2[, size])
This function is used to draw sample from a von Mises distribution.
Example:
import numpy as np import matplotlib.pyplot as plt m1, m2 = 0.0, 4.0 s1 = np.random.vonmises(m1, m2, 1000) from scipy.special import i0 plt.hist(s1, 50, density=True) x = np.linspace(-np.pi, np.pi, num=51) y = np.exp(m2*np.cos(x-m1))/(2*np.pi*i0(m2)) plt.plot(x, y, linewidth=2, color='r') plt.show()
Output:
31) wald(mean, scale[, size])
This function is used to draw sample from a Wald, or inverse Gaussian distribution.
Example:
import numpy as np import matplotlib.pyplot as plt h = plt.hist(np.random.wald(3, 3, 100000), bins=250, density=True) plt.show()
Output:
32) weibull(a[, size])
This function is used to draw sample from a Weibull distribution.
Example:
import numpy as np import matplotlib.pyplot as plt from scipy import special x=2.0 s=np.random.weibull(x, 1000) a = np.arange(1, 100.)/50. def weib(x, n, a): return (a/n)*(x/n)**np.exp(-(x/n)**a) count, bins, ignored = plt.hist(np.random.weibull(5.,1000)) a= np.arange(1,100.)/50. scale = count.max()/weib(x, 1., 5.).max() scale = count.max()/weib(a, 1., 5.).max() plt.plot(x, weib(x, 1., 5.)*scale) plt.show()
Output:
33) zipf(a[, size])
This function is used to draw sample from a Zipf distribution.
Example:
import numpy as np import matplotlib.pyplot as plt from scipy import special x=2.0 s=np.random.zipf(x, 1000) count, bins, ignored = plt.hist(s[s<50], 50, density=True) a = np.arange(1., 50.) b= a**(-x) / special.zetac(x) plt.plot(a, b/max(b), linewidth=2, color='r') plt.show()
Output: