#!/usr/bin/env ruby
# Russian roulette simulation, by Kimmo Kulovesi
num_simulations = 100000
max_players = 10
num_chambers = 6
each_player_spins = false
2.upto(max_players) do |num_players|
print "#{num_players} players"
print ' (' + (each_player_spins ? 'each' : 'first') + ' spins)'
$stdout.flush
wins = 0
num_simulations.times do
first_alive = true
players_left = num_players
while first_alive and players_left > 1
bullet_index = rand(num_chambers) + 1
current_player = 0
current_index = 0
while bullet_index != current_index
current_player += 1
current_player = 1 if current_player > players_left
if each_player_spins
current_index = rand(num_chambers) + 1
else
current_index += 1
end
end
first_alive = (current_player != 1)
players_left -= 1
end
wins += 1 if first_alive
end
print ": #{wins} wins out of #{num_simulations}"
print " -> #{wins.to_f / num_simulations}\n"
end