LINUX.ORG.RU

История изменений

Исправление melkor217, (текущая версия) :

dguzanov@dan-workstation:~$ cat coins.rb 

chests = [
          [:silver, :gold],
          [:gold, :gold],
          [:silver, :silver]
         ]
goldcount = 0
silvercount = 0

for i in 0..1000000 do
  rand_num = rand(0..2)
  randomchest = chests[rand_num]

  first_item = rand(0..1)
  second_item = 1 - first_item
  if randomchest[first_item] == :gold
    if randomchest[second_item] == :gold
      goldcount += 1
    elsif randomchest[second_item] == :silver
      silvercount += 1
    end
  end
end

puts "#{goldcount} gold coins"
puts "#{silvercount} silver coins"
dguzanov@dan-workstation:~$ ruby coins.rb 
332931 gold coins
166972 silver coins

Исправление melkor217, :

dguzanov@dan-workstation:~$ cat coins.rb 

chests = [
          [:silver, :gold],
          [:gold, :gold],
          [:silver, :silver]
         ]
goldcount = 0
silvercount = 0

for i in 0..1000000 do
  rand_num = rand(0..2)
  randomchest = chests[rand_num]

  first_item = rand(0..1)
  second_item = 1 - first_item
  if randomchest[first_item] == :gold
    if randomchest[second_item] == :gold
      goldcount += 1
    elsif randomchest[second_item] == :silver
      silvercount += 1
    end
  end
end

puts "#{goldcount} gold coins"
puts "#{silvercount} silver coins"
dguzanov@dan-workstation:~$ ruby coins.rb 
332931 gold coins
166972 silver coins

Исходная версия melkor217, :

порошок уходи

dguzanov@dan-workstation:~$ cat coins.rb

chests = [ [:silver, :gold], [:gold, :gold], [:silver, :silver] ] goldcount = 0 silvercount = 0

for i in 0..1000000 do rand_num = rand(0..2) randomchest = chests[rand_num]

first_item = rand(0..1) second_item = 1 - first_item if randomchest[first_item] == :gold if randomchest[second_item] == :gold goldcount += 1 elsif randomchest[second_item] == :silver silvercount += 1 end end end

puts «#{goldcount} gold coins» puts «#{silvercount} silver coins» dguzanov@dan-workstation:~$ ruby coins.rb 332931 gold coins 166972 silver coins