Skip to content

Instantly share code, notes, and snippets.

@FelixWolf
Last active May 21, 2016 07:11
Show Gist options
  • Save FelixWolf/6347c4c283a97d740977f01bda9ba461 to your computer and use it in GitHub Desktop.
Save FelixWolf/6347c4c283a97d740977f01bda9ba461 to your computer and use it in GitHub Desktop.
Does the rolling of two dice have a higher probability of landing on an even number or is @Tuurtles lying..?
#!/usr/bin/env python3
#MIT License
#
#Copyright (c) 2016 Félix Wolf
#
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all
#copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#SOFTWARE.
#To prove something from @Direboar's chat on twitch
#Does the rolling of two dice have a higher probability of landing on an even
# number or is @TUURTLES lying..?
import random
import datetime
#Times to run simulation
testCount = 10
loopCount = 1000
#Since statistics module is giving me a hard time
def getAverage(inlist):
total = 0
for i in inlist:
total = total + i;
return total / len(inlist)
totalEven = []
averageEven = 0
totalOdd = []
averageOdd = 0
startTime = datetime.datetime.now()
for t in range(testCount):
print("Running test %i"%t)
countEven = 0
countOdd = 0
for i in range(loopCount):
diceA = random.randint(1,6)
diceB = random.randint(1,6)
grandTotal = diceA + diceB
if grandTotal % 2 == 0: #Even
countEven = countEven + 1
else:
countOdd = countOdd + 1
totalEven.append(countEven)
totalOdd.append(countOdd)
endTime = datetime.datetime.now()
totalTime = endTime.microsecond - startTime.microsecond
print("Ran a total of %i tests with %i loops in %ims\nEven: %s\nOdd: %s" \
"\nAverage even: %i\nAverage odd: %i" % \
(testCount, loopCount, totalTime, \
", ".join(str(x) for x in totalEven), \
", ".join(str(x) for x in totalOdd), \
getAverage(totalEven), getAverage(totalOdd)))
@FelixWolf
Copy link
Author

Ran a total of 10 tests with 1000 loops in 56007ms
Even: 491, 508, 495, 484, 483, 483, 490, 472, 501, 486
Odd: 509, 492, 505, 516, 517, 517, 510, 528, 499, 514
Average even: 489
Average odd: 510

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment