| welcome to social.hackers | posts are made by Clocwork and Shadowdrifter | added some new hack diaries + podcasts |

Wednesday, May 11, 2011

Simple coin flip simulation.

By simple coin flip simulation I am meaning that I created a program with Python to "simulate" flipping a coin 100 hundred times which then returns the result. It is a simple program using list, and the random module built into Python.


import random
# Assigning the variables
i = 1
h = 1
t = 2
lit = []
lih = []
# Starting the loop
while 101 > i:
# Random function to select 1,2 to represent heads and tails.
data = random.randint(h,t)
print data
i = i + 1
data = int(data)

if data == 1:
lih.append("1")

if data == 2:

lit.append("2")

e = len(lit)
e = str(e)
f = len(lih)
f = str(f)
print 'You flipped tails ' + e + ' ' + 'times ' + 'and ' +  'You flipped heads ' + f + ' times'
raw_input()


This is just an example of what you can do with very simple functions, and just a few lines of code.

No comments:

Post a Comment