You are currently viewing Python Random uniform() Function

Python random.uniform() function is a part of the random module which is used to generate a random floating number between the two specified numbers. It takes two parameters(a, b) and returns a random floating number between a and b, inclusive of a but exclusive of b.

Advertisements

This is one of the common functions of the random module to generate random numbers in a certain range. Using random.randint() function we can generate random integers between specified ranges. In this article, I will explain Python random.uniform() function by using its syntax and parameters and generating the random floating number within a given range.

1. Quick Examples of random.uniform() Function

If you are in a hurry, below are some quick examples of random.uniform() function.


# Quick examples of uniform() function.

# Intitialize the range limits
a = 10
b = 20

# Example 1: Generate a floating random number
rand_num = random.uniform(a, b)

# Example 2: Generate multiple floating random numbers
rand_num = random.uniform(a, b)
for _ in range(5):
    rand_num = random.uniform(a, b)
    print(rand_num)

# Example 3: Generate a floating random number
# Intitialize the range limits with float values
a = 4.7
b = 6.8
rand_num = random.uniform(a, b)

# Example 4: # Create an array of random elements 
np.random.seed(5)  
ran_arr = np.random.uniform(size = 3, low = 5, high = 10)  

2. Random uniform() Function

The random.uniform(a,b) from a random module is used to generate the random floating number from the given range of specified numbers, where a is inclusive but b is exclusive. It takes lower and upper limit numeric values as its parameters so that a random floating number is generated within this specified range.

Related: Generate Random Integers Between 0 and 9 in Python

2.1 Syntax of Random uniform() Function

Following is the syntax of uniform() function.


# Syntax of uniform() function
random.uniform(a, b)

2.2 Parameters

Following are the parameters of uniform() function.

  • a : (Required) Lower limit of the specified range.
  • b : (Required) Upper limit of the specified range.

2.3 Return Value

It returns a random floating number from the specified range.

3. Usage of Random uniform() Function

Let’s pass a specific range of integers(10,20) into random.uniform() to generate a single random floating number within a specific range. If you want multiple values, you need to run this statement multiple times.

Related: Generate Random Float Numbers in Python


# Import random module
import random

# Intitialize the range limits
a = 10
b = 20
print("Lower limit:", a)
print("Higher limit:", b)

# Generate random float value
rand_num = random.uniform(a,b)
print("Random Floating number:", rand_num)

Yields below output.

Python Random Uniform

4. Get Multiple Random Floating Numbers using uniform()

To get multiple float numbers between two numbers use the uniform() function within for loop, each iteration of the loop generates a new random value. Here is an example,


import random
# Initialize the float numbers
a = 10
b = 20
print("Lower limit:", a)
print("Higher limit:", b)

# Generate multiple floating random numbers
rand_num = random.uniform(a, b)
print("Random Floating number:")
for _ in range(5):
    rand_num = random.uniform(a, b)
    print(rand_num)

Yields below output.

Python Random Uniform

5. Using Float Numbers into uniform() Function

Let’s pass a specific range of float values(4.7, 6.8) into random.uniform() to generate a single random float within a specific range. If you want multiple values, you need to run this statement multiple times.


# Intitialize the range limits with float values
a = 4.7
b = 6.8
print("Lower limit:", a)
print("Higher limit:", b)
# Generate floating random number
rand_num = random.uniform(a, b)
print("Random Floating number:", rand_num)

# Output:
# Lower limit: 4.7
# Random Floating number: 5.421697323124949

6. Create Random Array using uniform() function

We can use np.random.uniform() function to get the NumPy array of random elements. Before going to create a NumPy array, we need to import the NumPy module, which is one of the liabrary of Python.

First, initialize the random number generator using the seed() function and then specify the size of an array and range with specified numbers and pass them into np.random.uniform() function.


import numpy as np  
np.random.seed(5)  

# Create an array of random elements  
ran_arr = np.random.uniform(size = 3, low = 5, high = 10)  
print("Random numbers of array is: ", ran_arr)  

# Output:
# Random numbers of array is:  [6.10996586 9.35366153 6.03359578]

7. Conclusion

In this article, I have explained Python random.uniform() function syntax, parameters, and usage of how we can generate a random floating number within the given range of values. And also explained use this function how to generate a NumPy array of random elements.

The random.uniform(a, b) returns a random floating-point number between a and b, inclusive of a but exclusive of b. The a and b parameters define the range for the random number.

Happy Learning!!

Related Articles