Numpy Random Uniform Function Explained in Python

Hello geeks and welcome in this article, we will cover the NumPy random uniform(). Along with that, for an overall better understanding, we will also look at its syntax and parameter. Then we will see the application of all the theory part through a couple of examples.

As we know, NumPy is a very vast and powerful module of python. It provides us with several functions and one of which is NumPy random uniform(). This function helps us by getting random samples from the uniform distribution of data. Then it returns the random samples in the form of a NumPy array. As we progress further in this article, we will develop a better understanding of this function. In the next, we will look at the syntax associated with it.

General formula to calculate probability density function of a uniform function is as follows: [P(x)=1/(b-a)]

SYNTAX OF NUMPY RANDOM UNIFORM()

numpy.random.uniform(low=0.0, high=1.0)

This is the general syntax of our function. In the next section we will be looking at the various parameters associated with it.

PARAMETERS OF NUMPY RANDOM UNIFORM()

1.HIGH: FLOAT OR ARRAY LIKE OF FLOATS

This parameter represents the upper limit for the output interval. By default it is set to 1.

2.LOW:FLOAT OR ARRAY LIKE OF FLOATS

This parameter represents the lower boundary for the input interval. By default it is set to 0.

3.SIZE: INT OR TUPLES OF INT

It is an optional parameter that decides the output shape. By default it is equal to none.

Return Type

OUT: NDARRAY OR SCALAR

In output the function returns the sample from the parameterized uniform distribution.

Example Explaining Numpy Random Uniform Function n Python

As we are done with all the theory portion related to NumPy random uniform(), in this section, we will be looking at how this function works and how it helps us achieve our desired output. We will start with an elementary level example and gradually move our way to more complicated examples.

import numpy as ppool
print(ppool.random.uniform(3,0))
0.9813783419439916

In the above example, at first, we have imported the NumPy module. After which we have used our syntax along with a print statement to get the desired output. Here we have considered a very simple example that doesn’t involve many complications. I hope this example has helped you how to implement the syntax. As well as the general structure of the program.

Now let us see a more advanced example , that will help us in understanding better.

# import numpy 
import numpy as ppool 
import matplotlib.pyplot as plt 
pp = ppool.random.uniform(-1, 1, 1000) 
  
plt.hist(pp, bins = 40, density = True) 
plt.show()

#output

NumPy.random.uniform()

Above, we can see another example of our function. Instead of just calculating the value like in the 1st case, we have plotted its graph. To achieve it, we have used another powerful library of python that is Matplotlib. At first, we have imported the numpy library, following which we have imported Matplotlib. Then we used our function with its syntax to achieve our desired result. After this, the Matplot library comes into play. We have used this to draw or print a histogram using the data from our predefined function.

This function might be a bit difficult to understand. So I advise you to practice whole heartedly, because only then you will be able to achieve your desired result.

Must Read

CONCLUSION

In this article, we have covered the NumPy random uniform(). Besides that, we have also looked at its syntax and parameters. For better understanding, we looked at a couple of examples. We varied the syntax and looked at the output for each case. In the end, we can conclude that this function helps us by drawing samples from the uniform distribution. 

I hope this article was able to clear all Doubts. But in case you have any unsolved queries feel free to write them below in the comment section.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments