faizaand
11/30/2017 - 3:59 AM

A simple script I threw together quickly to run simple AP Statistics simulations with probability.

A simple script I threw together quickly to run simple AP Statistics simulations with probability.

# Faizaan Datoo Pd3
# A very simple example of using technology to automate simulations :)
# I /could/ have made it more complicated, but that would defeat the point!

import random

print "Enter a range of digits."

# Get those numbers...
minVal = input("\tYour minimum value: ")
maxVal = input("\tYour maximum value: ")
repetitions = input("\tAmount of repetitions: ")
ignoreRepeats = raw_input("\tIgnore repeats? (y/n): ")

print "Okay, crunching some numbers..."
tries = []

for i in range(repetitions):
    print "=== Repetition #" + str(i + 1) + " ==="
    times = 0
    history = []
    
    while True:
        # Generate random values, print them out
        generated = random.randrange(minVal, maxVal)

        # Check for duplicates if we have to!
        if ignoreRepeats == "y":
            while (generated in history):
                generated = random.randrange(minVal, maxVal)
            history.append(generated)
            
        print str(generated) + " ",

        # Increment the times value to show we chose another guy
        times += 1
        
        # If it's less than 8 (i.e. 01 to 07), we got it, so end the repetition
        if(generated < 8): 
            print "| " + str(times) + " tries"
            tries.append(times)
            break

# Get the mean and print it, we're done
mean = sum(tries) / float(len(tries))
print "It took an average of " + str(mean) + " tries overall."