Created
March 19, 2012 22:13
-
-
Save jonelf/2127599 to your computer and use it in GitHub Desktop.
Labouchère system simulation in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A Ruby version of the Labouchère system simulation in | |
# CoffeeScript by Anders Löfgren that can be found at | |
# https://github.com/gnidde/gniddegit/blob/master/roulette.coffee | |
# 2012-03-19 [email protected] | |
TABLE_ZEROS = 1 | |
MAX_BET = 24 | |
RED_NUMBERS = [1,3,5,7,9,12,14,16,18,19,21,23,25,27,30,32,34,36] | |
class Player | |
attr_accessor :stake_name, :current_betmin, :current_betmax, :current_earnings | |
def initialize(stake_name) | |
@stake_name = stake_name | |
@current_betmin = 1 | |
@current_betmax = 4 | |
@current_earnings = 0 | |
end | |
def calculate_earnings(result) | |
# Set correct bet | |
current_bet = @current_betmin + @current_betmax | |
# Subtract the bet from earnings | |
@current_earnings -= current_bet | |
# Check if we won | |
if result.include? @stake_name | |
@current_earnings += 2*current_bet | |
@current_betmax += 1 | |
else | |
@current_betmin += 1 | |
@current_betmax -= 1 | |
end | |
# Check if we need to start over from the beginning the next round | |
if @current_betmin >= @current_betmax || @current_betmin + @current_betmax > MAX_BET | |
@current_betmin = 1 | |
@current_betmax = 4 | |
end | |
end | |
end | |
def roll(players) | |
result = [] | |
# Let's roll the marble! | |
hit = Random.new.rand(1..36+TABLE_ZEROS) - TABLE_ZEROS | |
if hit > 0 | |
result.push RED_NUMBERS.include?(hit) ? :red : :black | |
result.push hit < 19 ? :low : :high | |
result.push hit % 2 == 0 ? :even : :odd | |
else | |
@nr_of_zeros_hit += 1 | |
end | |
players.each {|player| player.calculate_earnings result} | |
end | |
players = [Player.new(:red), Player.new(:black), | |
Player.new(:low), Player.new(:high), | |
Player.new(:even), Player.new(:odd)] | |
@nr_of_zeros_hit = 0 | |
# Let's roll some marbles | |
(1..1000000).each do |i| | |
roll players | |
if i%100000 == 0 | |
total_earnings = players.map(&:current_earnings).reduce(:+) | |
puts "#{i} : Total earnings: #{total_earnings}. Nr of zeros hit: #{@nr_of_zeros_hit}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment