Package: mcrand
MCRandMCRand is a library of Monte Carlo methods. Multidimensional integration, non-uniform random number generation, etc.Random Number GeneratorIn thesamplesfolder you can find a comparison between MCRand and Numpy for different probability distributions. Moreover, we use the program to generate random samples drawn from non-standard distributions. Anyway, we show here a quick guide to use MCRand library to generate the outputs ofsamples/pdf.py.To use the MCRand library to generate random numbers we first need to import the random generator (RandGen). This step can be done in the following wayfrommcrandimportsampleGaussian distributionTo generate gaussian distributed numbers with the MCRand random generator we first need to define the Gaussian PDFdefgaussian(x,mu,sigma):return(1/(np.sqrt(2*np.pi*sigma**2)))*np.exp(-(x-mu)**2/(2*sigma**2))Then, MCRand can be used to generateNgaussian numbers fromx0toxfas followsx0=-5xf=5N=1000sigma=1mu=0gaussian_sample=sample(gaussian,x0,xf,N,mu,sigma)Finally to plot the histogram and the PDF we can usematplotlib.pyplotimportmatplotlib.pyplotaspltplt.hist(gaussian_sample,bins=30,density=True,color=(0,1,0,0.5),label='MCRand sample')plt.plot(x,gaussian(x,mu,sigma),color='r',label=r'Gaussian PDF $\mu=%.2f$, $\sigma=%.2f$'%(mu,sigma))Cauchy distributionTo generate a Cauchy distribution we need to define its PDFdefcauchy(x,x0,gamma):return1/(np.pi*gamma*(1+((x-x0)/(gamma))**2))and then use the random number generator of MCRand as beforex0=-10xf=10N=10**5x0_cauchy=0gamma=1cauchy_sample=sample(gaussian,x0,xf,N,mu,sigma)Finally we plot the histogram and the PDFplt.hist(cauchy_sample,bins=50,density=True,color=(0,1,0,0.5),label='MCRand sample')plt.plot(x,cauchy(x,x0_cauchy,gamma),color='r',label=r'Cauchy PDF $\gamma=%.2f$, $x_0=%.2f$'%(gamma,x0_cauchy))From now on, we'll just write some code along with the output figures of thepdf.pyfile.Exponential distributiondefexponential(x):returnnp.exp(-x)x0=0xf=10N=10**5rand=sample(exponential,x0,xf,N)plt.hist(numpy_rand,bins=30,density=True,color=(0,0,1,0.8),label='NumPy sample')plt.hist(rand,bins=30,density=True,color=(0,1,0,0.5),label='MCRand sample')Rayleigh distributiondefrayleigh(x,sigma):return(x*np.exp(-(x**2)/(2*sigma**2)))/(sigma**2)x0=0xf=4sigma=1N=10**5rand=sample(rayleigh,x0,xf,N,sigma)plt.hist(rand,bins=30,density=True,color=(0,1,0,0.5),label='MCRand sample')plt.plot(x,rayleigh(x,sigma),color='r',label=r'Rayleigh PDF $\sigma=%.2f$'%sigma)Maxwell-Boltzmann distributiondefmaxwell_boltzmann(x,sigma):return(np.sqrt(2/np.pi))*(x**2*np.exp(-(x**2)/(2*sigma**2)))/(sigma**3)x0=0xf=10sigma=2N=10**5rand=sample(maxwell_boltzmann,x0,xf,N,sigma)plt.hist(rand,bins=30,density=True,color=(0,1,0,0.5),label='MCRand sample')plt.plot(x,maxwell_boltzmann(x,sigma),color='r',label=r'Maxwell-Boltzmann PDF $\sigma=%.2f$'%sigma)Symmetric Maxwell-Boltzmann distributiondefsymmetric_maxwell_boltzmann(x,sigma):return0.5*(np.sqrt(2/np.pi))*(x**2*np.exp(-(x**2)/(2*sigma**2)))/(sigma**3)x0=-10xf=10sigma=2N=10**5rand=sample(symmetric_maxwell_boltzmann,x0,xf,N,sigma)plt.hist(rand,bins=40,density=True,color=(0,1,0,0.5),label='MCRand sample')plt.plot(x,symmetric_maxwell_boltzmann(x,sigma),color='r',label=r'Maxwell-Boltzmann PDF $\sigma=%.2f$'%sigma)Modified Rayleigh distributionFinally we consider a invented probability distribution, given by the Rayleigh distribution multiplied byx. In some way we making a symmetric Rayleigh distribution. Then, this new distribution must be normalized, so the following equation must be acomplished:By nummeric integration it turns out that the normalization constant must beC=1/2.506628. Then we get the probability density function for this distribution.Therefore, MCRand can be used to generate random numbers distributed following this distribution as followsdefinvented(x,sigma):return(x**2*np.exp(-(x**2)/(2*sigma**2)))/(2.506628*sigma**2)x0=-4xf=4sigma=1N=10**5rand=sample(invented,x0,xf,N,sigma)plt.hist(rand,bins=40,density=True,color=(0,1,0,0.5),label='MCRand sample')plt.plot(x,invented(x,sigma),color='r',label=r'Modified Rayleigh PDF $\sigma=%.2f$'%sigma)Multidimensional IntegrationTo use the MCRand library to perform multidimensional integrals we first need to import the Integrate module. This step can be done in the following wayfrommcrandimportuniform_integrationThen, we must define the function to integrate in an NumPy ndarray supported way, so it must be defined generally. For instance let's imagine we want so solve the following integral:Then we should define the function asdeffunc(x):returnnp.sum(np.power(x,2))so each element of the x array will represent a variable.Finally, to get the result with  its error we can run the following codex0=[0,0]xf=[2,3]N=10**6result=uniform_integration(func,x0,xf,N)print(result)The result is given in the following format(25.99767534344232,0.02023068196284685)