๐Ÿ“ Python

Controlling Time and Randomness in Python โฑ๏ธ๐ŸŽฒ

P
Author
PyLand Team
๐Ÿ“…
Published
30.03.2026
โฑ๏ธ
Reading time
2 min
๐Ÿ‘๏ธ
Views
427
๐ŸŒฑ
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

๐Ÿ“

if __name__ == "__main__": the Python entry point

The if name == "main" guard defines a Python program's entry point: it distinguishes direct...

๐Ÿ“… 14.08.2026 ๐Ÿ‘๏ธ 44
๐Ÿ“

strip() and lower(): Preparing User Text ๐Ÿงน

A user may enter the correct word with extra spaces or different letter case. Python...

๐Ÿ“… 11.08.2026 ๐Ÿ‘๏ธ 50
๐Ÿ“

ord(), chr(), and Cyclic Letter Shifts in Python ๐Ÿ”

A string contains characters, but a computer stores every character as a numeric code. Python...

๐Ÿ“… 09.08.2026 ๐Ÿ‘๏ธ 89
๐ŸŽ“ Continue learning

Courses that cover this material

Visit the course to apply this material in practice.

Python from Scratch: Build 7 Practical Projects Open course curriculum