Skip to content

Instantly share code, notes, and snippets.

@ItsBerry
Created July 11, 2016 21:19
Show Gist options
  • Save ItsBerry/386b82f5fd66bbcef9081ec45fda780d to your computer and use it in GitHub Desktop.
Save ItsBerry/386b82f5fd66bbcef9081ec45fda780d to your computer and use it in GitHub Desktop.
from random import randint
import unittest
class CoinFlipSimulator():
"""Simulate a coin flip of a specific amount of flips"""
def __init__(self, number_of_flips):
self.number_of_flips = number_of_flips
self.sides_list = []
def simulation(self):
"""Perform the actual coin flips"""
for _ in range(self.number_of_flips):
self.sides_list.append(randint(1,2))
def show_results(self):
"""Show the results of the coin flips"""
heads = self.sides_list.count(1)
tails = self.sides_list.count(2)
total = heads + tails
print("====================Results====================")
print("Heads: " + str(heads / total * 100))
print("Tails: " + str(tails / total * 100))
print("\n")
class TestCoinFlipSimulator(unittest.TestCase):
def setUp(self):
self.mil = CoinFlipSimulator(1000000)
def test_simulation:
self.mil.simulation()
heads_percent = self.sides_list.count(1) / len(self.sides_list) * 100
tails_percent = self.sides_list.count(2) / len(self.sides_list) * 100
self.assertAlmostEqual(heads_percent, 50)
self.assertAlmostEqual(tails_percent, 50)
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment