Python 3.15 Random Values

This notebook covers all three Popcorn Hacks, the four-question MCQ, and the independent randomized SFI backend QA simulator.

Popcorn Hack 1: Random SFI Record Number

random.randint(1, total_records) includes both endpoints, so every record number from 1 through 4 is reachable. Repeated values are normal because each random selection is independent.

Code Runner Challenge

Popcorn Hack 1 - Random SFI Record Number

View IPYNB Source
# CODE_RUNNER: Popcorn Hack 1 - Random SFI Record Number
# Repeating the selection demonstrates that valid random IDs can repeat and do not have to cycle in order.
import random

total_records = 4

# randint includes both endpoints, so every valid record ID can be selected.
for run_number in range(5):
    chosen_record = random.randint(1, total_records)
    print("Random record ID:", chosen_record)
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Popcorn Hack 2: Random Structured SFI Car Part

Selecting the entire dictionary with random.choice() keeps the product name, category, and spec number connected as one structured record.

Code Runner Challenge

Popcorn Hack 2 - Random SFI Part Record

View IPYNB Source
# CODE_RUNNER: Popcorn Hack 2 - Random SFI Part Record
# Keeping the fields together prevents a product name from being accidentally paired with another record's spec number.
import random

parts = [
    {"product_name": "Replacement Flywheels and Clutch Assemblies", "category": "Auto Racing", "spec_number": "1.1"},
    {"product_name": "Multiple Disc Clutch Assemblies", "category": "Drag Racing", "spec_number": "1.2"},
    {"product_name": "Racing Flywheel Record", "category": "Auto Racing", "spec_number": "2.1"}
]

# Choose one complete record instead of choosing fields separately.
chosen_part = random.choice(parts)
print("Product:", chosen_part["product_name"])
print("Category:", chosen_part["category"])
print("Spec number:", chosen_part["spec_number"])
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Popcorn Hack 3: Random SFI API Test

The QA test randomly selects both a record and an API action, then routes the result through action-specific backend behavior.

Code Runner Challenge

Popcorn Hack 3 - Random SFI API Test

View IPYNB Source
# CODE_RUNNER: Popcorn Hack 3 - Random SFI API Test
import random

parts = [
    {"product_name": "Replacement Flywheels", "spec_number": "1.1"},
    {"product_name": "Multiple Disc Clutch Assemblies", "spec_number": "1.2"},
    {"product_name": "Racing Flywheel Record", "spec_number": "2.1"}
]
actions = ["GET/search", "POST/create", "PUT/update"]
stored_specs = ["1.1", "2.1"]

# Each run independently chooses a record and an API action.
part = random.choice(parts)
action = random.choice(actions)
print("Selected:", part["product_name"], "|", action)

# Route the selected action through the matching backend-style response.
if action == "GET/search":
    print("SEARCH RESULT:", part["product_name"], part["spec_number"])
elif action == "POST/create":
    if part["spec_number"] in stored_specs:
        print("CREATE REJECTED: duplicate spec", part["spec_number"])
    else:
        print("CREATE ACCEPTED:", part["spec_number"])
else:
    print("UPDATE TEST:", part["spec_number"])
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

MCQ 3.15

Answers: C, B, B, B
Score: 4/4

  1. C: randint(2, 5) can return 2, 3, 4, 5.
  2. B: independent random selections are allowed to repeat.
  3. B: random.randint(1, 4) matches inclusive RANDOM(1,4).
  4. B: one run may show only one of many possible outcomes.

Homework Hack: SFI Backend QA Simulator

The simulator performs five randomized tests using structured records and four backend actions, including duplicate-spec handling for create operations.

Code Runner Challenge

3.15 Homework - SFI Backend QA Simulator

View IPYNB Source
# CODE_RUNNER: 3.15 Homework - SFI Backend QA Simulator
import random

parts = [
    {"product_name": "Replacement Flywheels", "category": "Auto Racing", "spec_number": "1.1"},
    {"product_name": "Multiple Disc Clutch Assemblies", "category": "Drag Racing", "spec_number": "1.2"},
    {"product_name": "Racing Flywheel Record", "category": "Auto Racing", "spec_number": "2.1"}
]
actions = ["GET/search", "POST/create", "PUT/update", "DELETE/remove"]
stored_specs = ["1.1", "2.1"]
runs = 5

# Repeat the QA process because one random run cannot show every possibility.
for run_number in range(1, runs + 1):
    part = random.choice(parts)
    action = random.choice(actions)
    print("\nTest", run_number)
    print("Record:", part["product_name"], "| Spec:", part["spec_number"], "| Action:", action)

    # Each action follows a separate backend-style path.
    if action == "GET/search":
        print("SEARCH: found", part["product_name"])
    elif action == "POST/create":
        if part["spec_number"] in stored_specs:
            print("CREATE REJECTED: duplicate spec number")
        else:
            stored_specs.append(part["spec_number"])
            print("CREATE ACCEPTED: spec stored")
    elif action == "PUT/update":
        if part["spec_number"] in stored_specs:
            print("UPDATE ACCEPTED: existing record can be updated")
        else:
            print("UPDATE REJECTED: record does not exist")
    else:
        if part["spec_number"] in stored_specs:
            print("DELETE TEST: existing record would be removed")
        else:
            print("DELETE TEST: no matching stored record")
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Randomness Explanation

The possible record IDs are limited to the valid inclusive range, but repeated values are expected because the selections are independent. Testing a random program several times gives more evidence than one run, although no fixed number of random runs guarantees that every possible record/action combination will appear.