Python Random uniform Function

The uniform function in Python's random module returns a random floating-point number between two given numbers. This function is useful when you need to generate a random decimal number within a specific range.

Table of Contents

  1. Introduction
  2. uniform Function Syntax
  3. Examples
    • Basic Usage
    • Generating Multiple Random Numbers
  4. Real-World Use Case
  5. Conclusion

Introduction

The uniform function in Python's random module generates a random floating-point number within a specified range. This is useful for simulations, games, and any application where you need a random decimal value.

uniform Function Syntax

Here is how you use the uniform function:

import random
random.uniform(a, b)

Parameters:

  • a: The lower bound (inclusive).
  • b: The upper bound (inclusive).

Returns:

  • A random floating-point number N such that a <= N <= b.

Raises:

  • TypeError: If a or b are not numeric types.

Examples

Basic Usage

Here are some examples of how to use uniform.

Example

import random

# Generating a random float between 1.0 and 10.0
result = random.uniform(1.0, 10.0)
print("Random float between 1.0 and 10.0:", result)

# Generating a random float between -5.0 and 5.0
result = random.uniform(-5.0, 5.0)
print("Random float between -5.0 and 5.0:", result)

# Generating a random float between 0.0 and 1.0
result = random.uniform(0.0, 1.0)
print("Random float between 0.0 and 1.0:", result)

Output:

Random float between 1.0 and 10.0: 8.639054349345347
Random float between -5.0 and 5.0: -3.2360882390894607
Random float between 0.0 and 1.0: 0.8481663951918582

Generating Multiple Random Numbers

This example shows how to generate a list of random floating-point numbers using uniform.

Example

import random

# Generating a list of 5 random floats between 1.0 and 10.0
random_floats = [random.uniform(1.0, 10.0) for _ in range(5)]
print("List of random floats between 1.0 and 10.0:", random_floats)

Output:

List of random floats between 1.0 and 10.0: [2.751702676493436, 4.842698976205291, 4.554625117761608, 8.223941650149868, 8.581807957957604]

Real-World Use Case

Simulating Continuous Data

In real-world applications, the uniform function can be used to simulate continuous data, such as generating random sensor readings or modeling random measurements.

Example

import random

def simulate_sensor_readings(num_readings, lower_bound, upper_bound):
    return [random.uniform(lower_bound, upper_bound) for _ in range(num_readings)]

# Example usage
num_readings = 10
lower_bound = 20.0
upper_bound = 25.0

sensor_readings = simulate_sensor_readings(num_readings, lower_bound, upper_bound)
print("Simulated sensor readings:", sensor_readings)

Output:

Simulated sensor readings: [22.63263300707343, 24.246067636983057, 21.523963929827566, 24.01849666440701, 21.03566894799593, 23.39017747002631, 23.596797239437223, 22.138809714894634, 24.871820897047506, 23.466191278727837]

Conclusion

The uniform function in Python's random module generates random floating-point numbers within a specified range. This function is essential for various applications that require random decimal values, such as simulations, games, and modeling. By understanding how to use this method, you can efficiently generate random floats for your projects and applications.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare