📝 Python

Controlling Time and Randomness in Python ⏱️🎲

0
Author
04e5cc8b-58ac-4bdc-bdee-661bbb
📅
Published
30.03.2026
⏱️
Reading time
2 min
👁️
Views
116
🌱
Level
Beginner

Sometimes code runs too fast (all the text dumps on screen in a fraction of a second), or you need a game to behave unpredictably by picking random options.

Python provides two handy toolboxes for exactly these things (developers call them modules, or libraries): time (for time) and random (for randomness).

🧰 How to open the toolboxes? Using import

Tools you don’t use every day are stored inside Python in special packages. To tell Python “Hey, I need the time-handling package!”, you use the special import command.

import is usually written at the very top of your program.

import time
import random

# Now we can use these modules below

⏳ The time module: pausing execution

The most common and useful function from the time package is time.sleep(). It works like a pause button. Pass it a number of seconds, and the program literally freezes for that duration.

This is super useful for cool animations or gradual text output (the typewriter effect).

import time

print("System starting up...")
time.sleep(1)  # Wait 1 second
print("Checking engines...")
time.sleep(2)  # Wait 2 seconds
print("Liftoff!")

You can also use decimals (for short delays). For example, time.sleep(0.5) pauses the program for half a second.


🎲 The random module: adding unpredictability

When building games we need randomness (monster damage between 5 and 10, or a coin flip). That’s where the random package comes in.

The most useful function here is random.randint() — short for random integer.

Pass it two numbers: “from” and “to” (both inclusive).

import random

lucky_number = random.randint(1, 100)
print(f"Your lucky number: {lucky_number}")

# Rolling a die
die_roll = random.randint(1, 6)
print(f"You rolled a {die_roll}!")

Watch out: always prefix with the module name

Every time you use sleep or randint, you must prefix it with the name of the package it came from:
- Write time.sleep(2), not just sleep(2)
- Write random.randint(1, 10), not just randint(1, 10)

This way Python won’t get confused and will find exactly the right function!


🎰 random.choice(): pick a random item from a list

Sometimes you don’t need a random number — you need a random pick from a fixed set of options. For example:
- A random greeting (“Hi!”, “Hey!”, “How’s it going?”)
- A random color for a game
- A random enemy name

That’s what random.choice() does — it picks one random element from a list!

import random

# A list of greetings
greetings = ["Hi!", "Hey!", "Yo!", "How's it going?"]
random_greeting = random.choice(greetings)
print(random_greeting)

# Every time the program runs, a different greeting is picked!

Practical example: random weather

import random

weather_options = ["☀️ Sunny", "🌧️ Rainy", "⛈️ Stormy", "❄️ Snowy"]
today_weather = random.choice(weather_options)
print(f"Today's weather: {today_weather}")

Difference between randint and choice

random.randint(1, 10) random.choice([1, 2, 3, 4, 5])
Random NUMBER in a range Random ELEMENT from a list
Numbers only Works with anything (text, numbers)
randint(1, 100) → 42 choice(["red", "blue"]) → “red”

When to use choice:
- ✅ You already have a list of options
- ✅ You need random text, not a number
- ✅ Options aren’t consecutive (e.g. ["easy", "medium", "hard"])

When to use randint:
- ✅ You need a random number
- ✅ The range is a consecutive sequence (e.g. 1 to 100)
- ✅ You use it for math (damage values, health points)

Your reaction to the article

💬 Comments (0)

🔐 Sign in to leave a comment
🚪 Login
💭

No comments yet

Be the first to share your opinion about this article!

🔗 Similar

Similar articles

Continue learning with these materials

📝

Setting Up Your Environment: Python, pip, and VS …

Before writing code locally, you need to set up three tools: Python, pip, and VS...

📅 04.06.2026 👁️ 17
📝

The datetime Module: Working with Dates and Times

datetime is Python's standard module for working with dates and times. It's part of the...

📅 08.05.2026 👁️ 67
📝

.env Files and Environment Variables: Keeping Sec…

Imagine you wrote a program with an API key hardcoded in the source and pushed...

📅 08.05.2026 👁️ 76

Did you like the article?

Subscribe to our updates and receive new articles first. Grow with PyLand!