
Python Random Module
Hello, brave space explorers of Gdynia Space Academy! Today, we'll learn how to let fate decide your destination using Python's random module. After all, some of the greatest discoveries in space happened by chance!
Understanding Our Mission Parameters
Before we launch into the unknown, we need to understand two key components:
- The Python
random
module - our cosmic dice - Our solar system data - our possible destinations
Let's start by importing our space navigation tools:
import random
First, let's create our solar system database. We'll store interesting facts about each planet:
planets = {
"Mercury": {"distance": 77.3, "moons": 0, "surface": "Rocky"},
"Venus": {"distance": 38.2, "moons": 0, "surface": "Volcanic"},
"Mars": {"distance": 225.0, "moons": 2, "surface": "Desert"},
"Jupiter": {"distance": 778.5, "moons": 95, "surface": "Gas Giant"},
"Saturn": {"distance": 1434.0, "moons": 83, "surface": "Gas Giant"},
"Uranus": {"distance": 2871.0, "moons": 27, "surface": "Ice Giant"},
"Neptune": {"distance": 4495.0, "moons": 14, "surface": "Ice Giant"}
}
Rolling the Cosmic Dice
Now comes the exciting part! Let's write a function that randomly selects our destination:
def select_random_destination():
# Choose a random planet
destination = random.choice(list(planets.keys()))
# Get planet details
planet_info = planets[destination]
return destination, planet_info
# Let's try our space lottery!
planet, details = select_random_destination()
Making It More Interesting
What if we want to add some mission parameters? Let's create a more advanced function:
def generate_mission():
planet, details = select_random_destination()
mission_duration = random.randint(100, 1000) # days
crew_size = random.randint(3, 8)
print(f"Mission Brief")
print(f"Destination: {planet}")
print(f"Distance: {details['distance']} million km")
print(f"Surface type: {details['surface']}")
print(f"Number of moons to explore: {details['moons']}")
print(f"Mission duration: {mission_duration} days")
print(f"Crew size: {crew_size} astronauts")
Random Module: Your Space Navigation System
Let's break down the random functions we've used:
- random.choice()
: Selects a random item from a sequence (like picking a card from a deck)
- random.randint()
: Gives us a random integer between two numbers (inclusive)
Other useful random functions for space exploration:
#For random floating-point numbers (useful for coordinates!)
random_coordinate = random.uniform(-180, 180)
#For random selection with weights (some planets might be better choices!)
weighted_choice = random.choices(
list(planets.keys()),
weights=[1, 1, 2, 3, 2, 1, 1], # Mars, Jupiter, and Saturn are preferred
k=1
)[0]
Try It Yourself!
Now you can generate your own space missions! Here's a complete example to run:
print("\nGenerating new mission...")
generate_mission()
Mission Control's Notes
Remember, space cadets:
- The
random
module uses a pseudo-random number generator - Always check your destination's details before launch!
Ready to explore the cosmos? Run this code and see where your next adventure takes you! Remember, in space exploration, sometimes the most exciting discoveries come from random chances.
import random
planets = {
"Mercury": {"distance": 77.3, "moons": 0, "surface": "Rocky"},
"Venus": {"distance": 38.2, "moons": 0, "surface": "Volcanic"},
"Mars": {"distance": 225.0, "moons": 2, "surface": "Desert"},
"Jupiter": {"distance": 778.5, "moons": 95, "surface": "Gas Giant"},
"Saturn": {"distance": 1434.0, "moons": 83, "surface": "Gas Giant"},
"Uranus": {"distance": 2871.0, "moons": 27, "surface": "Ice Giant"},
"Neptune": {"distance": 4495.0, "moons": 14, "surface": "Ice Giant"}
}
def select_random_destination():
# Choose a random planet
destination = random.choice(list(planets.keys()))
# Get planet details
planet_info = planets[destination]
return destination, planet_info
# Let's try our space lottery!
planet, details = select_random_destination()
def generate_mission():
planet, details = select_random_destination()
mission_duration = random.randint(100, 1000) # days
crew_size = random.randint(3, 8)
print(f"Mission Brief")
print(f"Destination: {planet}")
print(f"Distance: {details['distance']} million km")
print(f"Surface type: {details['surface']}")
print(f"Number of moons to explore: {details['moons']}")
print(f"Mission duration: {mission_duration} days")
print(f"Crew size: {crew_size} astronauts")
generate_mission()