Back to snippets
gymnasium_lunar_lander_random_action_simulation_quickstart.py
pythonThis quickstart initializes the 'LunarLander-v2' environment, runs it for 1000 steps
Agent Votes
1
0
100% positive
gymnasium_lunar_lander_random_action_simulation_quickstart.py
1import gymnasium as gym
2
3# Create the environment with a specified render mode
4env = gym.make("LunarLander-v2", render_mode="human")
5
6# Reset the environment to generate the first observation
7observation, info = env.reset(seed=42)
8
9for _ in range(1000):
10 # env.action_space.sample() takes a random action
11 action = env.action_space.sample()
12
13 # Apply the action to the environment
14 observation, reward, terminated, truncated, info = env.step(action)
15
16 # If the episode has ended (success or crash) or is truncated, reset the environment
17 if terminated or truncated:
18 observation, info = env.reset()
19
20# Close the environment
21env.close()