Space Python Loading Python…

Seeded simulation

Write the codeEasy 2 stages

Before trusting the new glitch filter, the team wants to throw simulated glitches at it: 100 readings, each with a 5% chance of going wrong. Random data is easy to make. The trouble starts when a test fails once and then passes when you run it again.

Make the simulation random, but the same random every time.

Seed random with 42. Then, for each of 100 readings, count a glitch when random.random() < 0.05, and print the count:

Glitches in 100 readings: 7

Call random.random() exactly once per reading, after the seed: the check expects that exact count, and any other order of calls gives a different one.

The exact count

Finish stage 1 to unlock this stage.

For each glitch, pick its kind with random.choice(KINDS), right after the check that found it, count the kinds in a dict called by_kind, and print them sorted after the total:

Glitches in 100 readings: 5
DROPOUT: 3
SPIKE: 2

Call random.choice() only for glitches. Look at the total: it is no longer 7.

Repeatable kinds
You're not logged in, so your progress is kept in this browser only. Log in to save it, and to use hints and solutions.

Hints

Import System Modules

import-system-modules.py
# A module is a toolbox someone else already built and tested for you

# Plain import brings in the whole module
import sys
import platform
import math

# from ... import brings in just one tool
from math import sqrt

# as gives a module a shorter nickname
import random as rng

# With a plain import: module name, a dot, then the tool
print("Python version:", platform.python_version())
print("Running on:", platform.system())
print("Python lives at:", sys.executable)

# math carries constants you would not want to type from memory
radius_km = 6371
print("Earth circumference:", round(2 * math.pi * radius_km), "km")

# sqrt arrived on its own, so it needs no dot in front
print("Square root of 9:", sqrt(9))

# rng is simply a nickname for random
print("Random sol of the Martian year:", rng.randint(1, 687))
Open the full example
script.py
PythonLn 1, Col 1Spaces: 4
Loading the Python environment…

Separate arguments with commas. They're passed on every Run as sys.argv[1:].