# Rayleigh Distributon in Python

## In this tutorial you will learn:

• What is Rayleigh Distribution?
• Rayleigh Distribution Implementation in python
• Visualization of Rayleigh Distribution

## Rayleigh Distribution

Rayleigh Distribution is a continuous probability distribution named after English Lord Rayleigh. This distribution is for non-negative random variables and it is essentially a chi square distribution with two degrees of freedom. Rayleigh distribution is used to model the wind speeds, wave heights and sound radiations. It also gives measure the life time of electrical components like capacitors, resistors, transistors etc.

## Rayleigh Distribution in Python

The random module of python’s NumPy library provide an inbuilt function rayleigh() for implementation of Rayleigh Distribution. The rayleigh() function takes in two parameters, first parameter is the “size” of the array which we require as an output and it is the mandatory parameter. The second parameter “scale” is an optional parameter and it is the standard deviation which decides the flatness of distribution, if this parameter is not explicitly defined its value is taken as 1.0 by default.
Lets take an example, we will generate a 1D array(1,5) of Rayleigh distribution with scale 2.0. In this code we can see that we are importing the random module in the second line of the code and in the fourth line we are applying the Rayleigh distribution with output size parameter (1,5) and standard deviation “scale” 2.0.

`#importing the random modulefrom numpy import random#applying the Rayleigh functionres_arr= random.rayleigh(size=4,scale=2.0)#printing the resultsprint('1D array of size(1,5) having Rayleigh distribution with scale 2.0 :\n')print(res_arr)`

Lets take another example, in this example we will generate a 2D array of Rayleigh distribution having size (2,5) with default standard deviation.
`#importing the random modulefrom numpy import random#here we are using Rayleigh function to generate Rayleigh distribution of size 2 x 5 with default scale valueres_arr = random.rayleigh(size=(2,5))print('2D Rayleigh Distribution as output from rayleigh() function:\n')#printing the resultprint(res_arr)`

In this example we will generate a 3D array of Rayleigh distribution of the size(2,4,2) with standard deviation 1.5
`#importing the random modulefrom numpy import random#here we are using Rayleigh function to generate Rayleigh distribution of size 2 x 4 x 2res = random.rayleigh(size=(2,4,2), scale=1.5)print('3D Rayleigh Distribution as output from rayleigh() function:\n')#printing the resultprint(res)`

## Visualization of Rayleigh Distribution

In this example we will visualize the Rayleigh Distribution with default value of standard deviation using the displot function of seaborn library.

`#importing all the required modules and packagesfrom numpy import randomimport matplotlib.pyplot as mplimport seaborn as sb#here we are using Rayleigh function to generate distributions of size 2000 with standard deviation 1sb.distplot(random.rayleigh(size=2000), hist=False, label='Rayleigh Distribution')#plotting the graphmpl.show()`

### Tags

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.