Skip to content

Instantly share code, notes, and snippets.

@fynntimes
Last active December 1, 2017 01:00
Show Gist options
  • Save fynntimes/07bf1d1ce61597bc4aae972cf5cecdf4 to your computer and use it in GitHub Desktop.
Save fynntimes/07bf1d1ce61597bc4aae972cf5cecdf4 to your computer and use it in GitHub Desktop.
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."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment