Generating Random Numbers in Python: random, uniform, and randint

We often need “random numbers” for simulations, game development, or creating test data.

Python has a standard library called the random module for this purpose. With this module, you can easily generate random numbers, such as decimals (floats) or integers within a specific range.

This article explains three major functions of the random module and how to create a list of random numbers.

Table of Contents

  1. List of Major Random Number Functions
  2. How to Use Each Function
      1. Float between 0.0 and 1.0: random.random()
      1. Float within a range: random.uniform(a, b)
      1. Integer within a range: random.randint(a, b)
  3. Application: Generating a List of Random Numbers
  4. Summary

目次

List of Major Random Number Functions

Here are the most frequently used functions in the random module.

Function NameDescriptionReturn Type
random.random()Generates a random float between 0.0 and 1.0 (0.0 <= x < 1.0).float
random.uniform(a, b)Generates a random float between a and b.float
random.randint(a, b)Generates a random integer between a and b (includes both a and b).int

How to Use Each Function

1. Float between 0.0 and 1.0: random.random()

This is the most basic function. It is often used for probability checks (e.g., a 30% chance of success).

import random

# Generate a random float: 0.0 <= x < 1.0
prob = random.random()

print(f"Generated value: {prob}")

# Example: 30% chance of winning
if prob < 0.3:
    print("Result: Win!")
else:
    print("Result: Lose")

Output Example:

Generated value: 0.284391...
Result: Win!

2. Float within a range: random.uniform(a, b)

Use this when you need a random float within a specific range. This is useful for simulating things like temperature or sensor data.

import random

# Simulate body temperature between 36.0 and 37.5 degrees
body_temp = random.uniform(36.0, 37.5)

# Display up to 2 decimal places
print(f"Measured Temperature: {body_temp:.2f} degrees")

Output Example:

Measured Temperature: 36.84 degrees

3. Integer within a range: random.randint(a, b)

Use this when you need an integer within a specific range, like rolling a dice. Note that it includes both the start value and the end value.

import random

# Roll a dice (1 to 6)
dice_roll = random.randint(1, 6)

print(f"Dice Roll: {dice_roll}")

Output Example:

Plaintext

Dice Roll: 4

Application: Generating a List of Random Numbers

You can use list comprehensions to efficiently create a list of random numbers.

Example: Generating Test Data

Here is an example of generating random scores (0 to 100) for 5 people.

import random

# Generate 5 random integers between 0 and 100
# Using '_' means we do not use the loop variable
scores = [random.randint(0, 100) for _ in range(5)]

print(f"Generated Score List: {scores}")

Output Example:

Generated Score List: [78, 45, 92, 10, 66]

Summary

  • random.random(): Basic random float (0.0 to <1.0).
  • random.uniform(a, b): Random float within a specific range.
  • random.randint(a, b): Random integer within a specific range (includes end value).

Choose the right function for your needs to implement the correct random behavior.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次