Python Coding Day 8 | Randomization, Random Integers, Random Floats

Randomization, Random Integers, Random Floats

Watch the lesson tutorial  ðŸ”»


🎲 Randomization in Python

Randomization is the process of generating an outcome that is unpredictable and non-deterministic, meaning the result isn't based on a fixed pattern or sequence. In computing, we often use pseudo-random numbers, which are generated by an algorithm but appear random for practical purposes.

In Python, the random module provides functions for working with random numbers, picking random items from sequences, and more.

Practical Example: Shuffling a Deck of Cards

A classic use of randomization is shuffling. The random.shuffle() function shuffles a list in place.

Python
import random

# A list representing a simple deck of cards
deck = ["Ace", "King", "Queen", "Jack", "10", "9", "8", "7", "6", "5", "4", "3", "2"]

print("Original Deck:", deck)

# Shuffling the deck
random.shuffle(deck) 

print("Shuffled Deck:", deck)

# You can also pick a random item from a sequence using random.choice()
random_card = random.choice(deck)
print("Random Card Picked:", random_card)

🔢 Random Integers: random.randint()

The function used to generate a random integer (whole number) within a specified range is random.randint(a, b).

  • It returns an integer $N$ such that $a \le N \le b$.

  • Crucially, both endpoints $a$ and $b$ are inclusive.

The specific instruction you gave, random.random - int, seems to be mixed up. The correct function for generating random integers is random.randint(). The random.random() function is used for floats (explained next).

Practical Example: Rolling a Die

To simulate rolling a standard six-sided die, you need a random integer between 1 and 6, inclusive.

Python
import random

# Simulate rolling a six-sided die
die_roll = random.randint(1, 6)

print(f"You rolled a {die_roll}") 

# Example 2: Simulating a lottery number (e.g., between 1000 and 9999)
lottery_number = random.randint(1000, 9999)
print(f"Your random 4-digit lottery number is: {lottery_number}")

🧮 Random Floats: random.random()

The function used to generate a random float (decimal number) is random.random().

  • It takes no arguments.

  • It returns a float $x$ such that $0.0 \le x < 1.0$.

  • The result is always greater than or equal to 0.0, but strictly less than 1.0.

The specific instruction you gave, random.randint - float, also seems to be mixed up. The correct function for generating random floats in the range $[0.0, 1.0)$ is random.random(). The random.randint() function is used for integers.

Practical Example: Generating a Probability Value

random.random() is often used to simulate probabilities or choose an action based on a percentage chance.

Python
import random

# Generate a random float between 0.0 and 1.0
probability_value = random.random()

print(f"Generated probability value: {probability_value:.4f}") 

# Practical application: Simulating a 30% chance of rain
if probability_value < 0.3:
    print("Looks like it will rain today! (Chance was 30%)")
else:
    print("The sun will shine! (Chance was 70%)")

# Note: To generate a float within a custom range (e.g., 5.0 to 10.0), 
# you would use random.uniform(5.0, 10.0)
custom_float = random.uniform(5.0, 10.0)
print(f"A random float between 5.0 and 10.0: {custom_float:.2f}")

- by Chirana Nimnaka

Comments

Popular posts from this blog

Python Coding Day 1 | The print() Function and Comments

What is Python?

Set Up an Environment to Code in Python