🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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
- Introduction
uniformFunction Syntax- Examples
- Basic Usage
- Generating Multiple Random Numbers
- Real-World Use Case
- 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
Nsuch thata <= N <= b.
Raises:
TypeError: Ifaorbare 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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment