S
Semih Ozkoseoglu
Hi,
I'm trying to teach my self ruby so i decided to write a simple roulette
that picks a rand number (0 to 36) then decides if its red or black. but
after i've written a version of the code i realized that the code was
biased towards black ( much easier to see over 1 million rolls)
Then to test this I've simplified everything and its still biased
against black. if i take 0 out of the equation and made use (1+rand(25))
the bias disappeared.
Anyway without further annoying you here is the code. If you spin it for
over a million you can easily see the bias (i tried up to 1000000000).
Thanks for any help. I know it's not a life or death situation but for
the life of me I just want to know why is this happening.
THE CODE:
# ARRAYS =======================
black = [2, 4, 6, 8, 10, 11, 13, 15, 17, 20, 22, 24, 26, 28, 29, 31, 33,
35]
red = [1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34,
36]
zero = [0]
# VARIABLES ===================
bl = 0
rd = 0
zr = 0
# # OF ROLLS ==================
puts 'How many rolls to simulate'
number_of_rolls = gets.chomp.to_i
# THE LOOP ====================
while number_of_rolls > 0
number = rand(36)
if
black.include?(number)
bl +=1
elsif
red.include?(number)
rd +=1
elsif
zero.include?(number)
zr +=1
end
number_of_rolls -=1
end
puts bl
puts rd
puts zr
Attachments:
http://www.ruby-forum.com/attachment/4066/working.rb
I'm trying to teach my self ruby so i decided to write a simple roulette
that picks a rand number (0 to 36) then decides if its red or black. but
after i've written a version of the code i realized that the code was
biased towards black ( much easier to see over 1 million rolls)
Then to test this I've simplified everything and its still biased
against black. if i take 0 out of the equation and made use (1+rand(25))
the bias disappeared.
Anyway without further annoying you here is the code. If you spin it for
over a million you can easily see the bias (i tried up to 1000000000).
Thanks for any help. I know it's not a life or death situation but for
the life of me I just want to know why is this happening.
THE CODE:
# ARRAYS =======================
black = [2, 4, 6, 8, 10, 11, 13, 15, 17, 20, 22, 24, 26, 28, 29, 31, 33,
35]
red = [1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34,
36]
zero = [0]
# VARIABLES ===================
bl = 0
rd = 0
zr = 0
# # OF ROLLS ==================
puts 'How many rolls to simulate'
number_of_rolls = gets.chomp.to_i
# THE LOOP ====================
while number_of_rolls > 0
number = rand(36)
if
black.include?(number)
bl +=1
elsif
red.include?(number)
rd +=1
elsif
zero.include?(number)
zr +=1
end
number_of_rolls -=1
end
puts bl
puts rd
puts zr
Attachments:
http://www.ruby-forum.com/attachment/4066/working.rb