Skip to content

Instantly share code, notes, and snippets.

@fynntimes
Last active December 1, 2017 01:00

Revisions

  1. fynntimes renamed this gist Dec 1, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. fynntimes created this gist Nov 30, 2017.
    47 changes: 47 additions & 0 deletions simulations.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    # 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."