# Rectangular Distribution in Python

## In this tutorial you will learn:

• What is a Rectangular Distribution?
• Rectangular Distribution Implementation in python
• Visualization of Rectangular Distribution

## Rectangular Distribution

Rectangular distribution is a distribution that has a constant probability, it is also known as uniform distribution. It is a family of symmetric probability distributions and the outputs of the distribution lies between certain bounds. These bounds are defined by two parameters which are the maximum and minimum values. The difference between the bounds defines the interval length, all intervals of the same length on the distribution's support are equally probable.

## Rectangular Distribution in Python

Rectangular distribution in python is implemented using uniform() function. The rectangular() function takes in three parameters. First parameter “size” is the mandatory parameter and it is size of the output array which could be multi dimensional. The second parameter “a” is the lower bound, and if not explicitly defined, its value is taken as 0. The third parameter “b” is the upper parameter and if not explicitly defined, its value is taken as 1.
Lets take an example, we will generate an 1D array(1,7) of rectangular distribution having the 7 as lower limit(a) and 14 as upper limit(b). Here you can observe that if we re run the program again and again, the values of the distribution will keep on changing.

`#importing the random modulefrom numpy import random#applying the uniform function with lower limit 7 and upper limit 14res_arr= random.uniform(low=7, high=14, size=7)#printing the resultsprint('1D array having Rectangular Distribution within limits 7 and 14:/n')print(res_arr)`

In this example we will generate a 2D array of rectangular distribution having size (3,4), in the first line of code we are importing random module from numpy library and in third line we are calling the uniform function with upper and lower limit as 2 and 6 respectively.
`#importing the random modulefrom numpy import random#here we are using uniform function to generate rectangular distribution of size 3 x 4 with lower limit 2 and upper limit 6res_arr = random.uniform(low = 2, high=6, size=(3,4))print('2D Rectangular Distribution as output from uniform() function:\n')#printing the resultprint(res_arr)`

Lets take another example, in this example we will generate a 3D array of rectangular distribution of the size(3,2,5) with default upper and lower limit.
`#importing the random modulefrom numpy import random#here we are using uniform function to generate uniform distribution of size 3 x 2 x 5res = random.uniform(size=(3,2,5))print('3D Rectangular Distribution as output from uniform() function:\n')#printing the resultprint(res)`

## Visualization of Rectangular Distribution

In this example, for our better understanding of rectangular distribution itself and the syntax, we will plot rectangular distribution on a graph. Here we will again use distplot() function for graph plot. The upper and lower limit will be 4 and 8 respectively, and the total of 50 values will be used for this plot. Here you can observe that the plot of graph is rectangular shaped, this is the reason behind the uniform distribution being called as rectangular distribution.

`#importing all the required modules and packagesfrom numpy import randomimport matplotlib.pyplot as mplimport seaborn as sb#here we are using Uniform function to generate Rectangular Distribution of size 100 with lower limit 4 and upper limit 8sb.distplot(random.uniform(size=100, low=4, high=8),hist=False)#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.