Skip to content

Instantly share code, notes, and snippets.

@luke14free
Last active January 20, 2016 09:29
Show Gist options
  • Save luke14free/c0be50d4e7c341a1270a to your computer and use it in GitHub Desktop.
Save luke14free/c0be50d4e7c341a1270a to your computer and use it in GitHub Desktop.
Randomic, yet efficient slot machine simulator/combination generator with custom win profiles, figures and payoffs.
import itertools
import random
els = range(10) # elements of the slot
wins_profile = dict()
wins_profile[0] = 65
# wins_profile[10] = 15 # don't know any valid combo for this :-)
# wins_profile[20] = 10 # same here
wins_profile[50] = 5
wins_profile[100] = 2
wins_profile[200] = 1
all_probs = reduce(lambda x, (k, v): x + [k] * v, wins_profile.items(), [])
def generate_prize():
prize = random.choice(all_probs)
return prize
def generate_play():
cols = {i: random.sample(els, 3) for i in range(5)}
return [i[j] for j in range(3) for i in cols.values()]
def _winning(lines, combination):
return len(set(itertools.compress(lines, combination))) == 1
def is_winning(lines, combinations):
for combination in combinations:
if _winning(lines, combination):
return True
return False
def is_winning_higher_prize(lines, combos, prize):
for other_prize in [_prize for _prize in combos.keys() if _prize > prize]:
if is_winning(lines, combos[other_prize]):
return True
return False
def find_combo(prize):
combos = generate_combos()
lines = generate_play()
while not is_winning(lines, combos[prize]) \
and not is_winning_higher_prize(lines, combos, prize):
lines = generate_play()
return lines
def generate_combos():
combos = {
0: """
.....
.....
.....""",
200: """
xxxxx
.....
.....
.....
xxxxx
.....
.....
.....
xxxxx
..x..
.x.x.
x...x
x...x
.x.x.
..x..""",
100: """
xxxx.
.....
.....
.....
xxxx.
.....
.....
.....
xxxx.
.xxxx
.....
.....
.....
.xxxx
.....
.....
.....
.xxxx
""",
50: """
xxx..
.....
.....
.....
xxx..
.....
.....
.....
xxx..
..xxx
.....
.....
.....
..xxx
.....
.....
.....
..xxx
xxx..
.....
.....
.....
xxx..
.....
.....
.....
..xxx
.xxx.
.....
.....
.....
.xxx.
.....
.....
.....
.xxx.
"""}
return {k: [[j == "x" for j in "".join(i.split("\n"))] for i in v.split("\n\n")] for k, v in combos.items()}
if __name__ == "__main__":
times = []
prizes = []
iterations = 10000
import time
for i in range(iterations):
s = time.time()
prize = generate_prize()
find_combo(prize)
times.append(time.time() - s)
prizes.append(prize)
print "Average time to find one solution (in seconds):", float(sum(times)) / float(len(times))
print "Average prize for %s iterations" % (iterations), float(sum(prizes)) / float(len(prizes))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment