## Random Numbers with Uniform Distribution

A sequence of numbers with a uniform distribution is one in which each number has an equal chance of being selected. These numbers are frequently used in simulations, statistical modeling, and other applications where an even distribution of numbers is required. Use the random.uniform() function from the Python random package to get random numbers with a uniform distribution. The function accepts two parameters, a and b which define the distribution’s range and returns a random floating-point number.

## Import Random Module

In Python, we used the NumPy library’s “random” module. To produce random numbers from a uniform distribution, use this module. Now, we will discuss the procedure for generating random numbers. First, open a Python tool like PyCharm, Spyder, or Jupiter Notebook and create a project where we can import Python libraries and perform uniform distribution. The code for importing the random module is given below.

You can also write this as:

or

import random as rd

Or whatever short form you want to give.

The Mersenne Twister serves as the main generator in the “Random” module. It generates precision floats with 53 bits.

## Syntax of Random Module

After importing the ‘random’ module successfully in a Python application, we will discuss the syntax of the random module to find a uniform distribution. The syntax of this module is:

To generate random numbers, the random.uniform method() is used. Remember, the random numbers are generated from the uniform distribution. When referring to the NumPy library, we use the alias “np” for the NumPy library. This method takes three parameters like a, b, and size.

Argument “a” displays a low interval, argument “b” displays a high interval, and argument “size” displays random numbers based on the size specified.

**Example # 1:**

Here, we will take a simple and linear example in which we will only take the lower bound and the upper bound. The reference code is provided.

import random

random_number=numpy.random.uniform(1.2 , 4.3)

uniform_distrib= print("The Random number from uniform distribution is:",random_number)

In this example, we import a NumPy library and then import a random module through which we can generate random numbers. Both of these libraries (NumPy and random) are very crucial for executing the sample codes mentioned in this article.

After that, we have called a uniform method. This function is primarily defined in the random module. Here, we took a range between “1.2”’ and “4.3” values and generated a random number between these ranges. The uniform method returns a value and we stored this value in the variable ‘random_number’. After that, we wanted to display this variable ‘random_number’ to the console. So, we passed this variable in a print statement. Lastly, we assigned a print statement value to the ‘uniform_distrib’ variable.

On running the code, the result is shown on the console. The result in textual form is given below:

Here, we cannot give any size value. That is why only one float value is shown on the console.

**Example # 2:**

Here, we will be discussing generating random numbers again. We will explain this along with the size of the numbers. The reference code related to this example is mentioned below:

import random

rand_Num=np.random.uniform(low=3.2 , high=6.5, size=3.3)

uniDistriRandNum= print("Random Numbers are:",rand_Num)

We imported the NumPy library and aliased it as “np” in this case. The random module is part of NumPy, so NumPy importation is very important. After import, we called the “uniform method,” in which we passed three arguments like the upper bound, lower bound, and size of this uniform distribution. The low bound in this case is 3.2, the high bound is 6.5, and the size is 3.3’. The uniform methods return a value and we stored this value in the variable “rand_Num”. After that, we passed this “rand_Num” in a print statement for showing the result on the console. So, in this way, we find random numbers from a uniform distribution.

The output of example 2 is shown here.

Here, we see that three random numbers are generated from our given range. These three random numbers are ‘6.16573794’, ‘5.34926847’, and ‘3.38366092’. So, in this way, we generated random numbers.

**Example # 3:**

In the last example of this article, the random module is highlighted again. See the code below. Here, we first import a NumPy library and then import a random module into these Python files. Following that, we will take ranges and between these ranges, we will generate random numbers. In line 3, we will call a random module with the uniform method in which we will pass 3 parameters like the lower bound, upper bound, and size of random numbers between these ranges. The lower bound value will be “-3.2,” the upper bound value will be “2.5,” and the size of the values will be “5”. The uniform functions return a value and we will store this value in the variable ‘rNum’. Now, we will push this variable to the print statement to show the result of the uniform method on the console. So, in this way, we successfully generate random numbers that are from a uniform distribution.

import random

rNum=np.random.uniform(low=-3.2 , high=2.5, size=5)

UniDri= print("Generate Random numbers from Uniform distribution:",rNum)

The output of example 3 is given below:

Generate Random numbers from Uniform distribution: [2.08222359 0.75018807 2.03184071 1.88274545 1.14929147].

## Conclusion

In this article, we discovered that we can easily generate random numbers from the uniform distribution using the NumPy library’s random module. In your Python applications, you can also use random. uniform() method example for practicing the process of generating a random number from the uniform distribution. As shown in this article, random numbers are generated from a uniform distribution which means all the values within the range have an equal chance of being generated. This concept is very useful in python applications like games or where ranges of random values are needed.