Roulette & rand

S

Semih Ozkoseoglu

One problem that I can see is that you're allowing your random betting
strategy to pick 0, rather than red or black, which I don't think is
your intention.

Change
number_02 = rand(37)
color_02 = colors[number_02]

to

color_02 = [:red, :black][rand(2)]

Hi everyone,
After I changed the code as Paul's note above the code started to
create identical results for both bet_rand and bet_red.
Thanks to everyone who helped. Its much appreciated.
The new code reads as this:


colors = {}
[2, 4, 6, 8, 10, 11, 13, 15, 17, 20, 22, 24, 26, 28, 29, 31, 33,
35].each do |i|
colors = :black
end
[1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36].each
do |i|
colors = :red
end
colors[0] = :zero

bet_rand = 0
bet_red = 0

puts 'How many rolls to simulate'
number_of_rolls = gets.chomp.to_i

number_of_rolls.times do

number_01 = rand(37)
color_01 = colors[number_01]
number_02 = rand(37)
if number_02 == [:red, :black]
color_02 = colors[number_02]
else
color_02 = :red
end

if color_01 == :red
bet_red += 1
else
bet_red -= 1
end

if color_02 == color_01
bet_rand += 1
else
bet_rand -= 1
end
end

puts bet_rand
puts bet_red
 
S

Semih Ozkoseoglu

Excuse my ignorance,

After running the above code a few times I realized that the bet_rand
and bet_red was always EXACTLY the same. So I realized that there was
something wrong with the code. bet_rand somehow behaves exactly the same
way as bet_red. I don't know whats wrong with it but it doesn't matter.
One problem that I can see is that you're allowing your random betting
strategy to pick 0, rather than red or black, which I don't think is
your intention.

Change
number_02 = rand(37)
color_02 = colors[number_02]

to

color_02 = [:red, :black][rand(2)]


Here is the fixed code with Paul's suggestion

colors = {}
[2, 4, 6, 8, 10, 11, 13, 15, 17, 20, 22, 24, 26, 28, 29, 31, 33,
35].each do |i|
colors = :black
end
[1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36].each
do |i|
colors = :red
end
colors[0] = :zero

bet_rand = 0
bet_red = 0

puts 'How many rolls to simulate'
number_of_rolls = gets.chomp.to_i

number_of_rolls.times do

number_01 = rand(37)
color_01 = colors[number_01]
color_02 = [:red, :black][rand(2)]

if color_01 == :red
bet_red += 1
else
bet_red -= 1
end

if color_02 == color_01
bet_rand += 1
else
bet_rand -= 1
end
end

puts bet_rand
puts bet_red
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,113
Latest member
Vinay KumarNevatia
Top