[SOLUTION] [QUIZ] Probable Iterations (#141)

M

MenTaLguY

My solution follows. Nothing fancy this time; just enough to do
the job. One feature of interest: it calculates the exact room
needed for the left-hand column in advance.

-mental

mode = nil
if ARGV[0] =~ /-([sv])/
mode = $1
ARGV.shift
end

if ARGV.length != 2
$stderr.puts "Usage: #{$0} [-s|-v] NUM_DICE MIN_FIVES"
exit 1
end

n_dice = ARGV[0].to_i
min_fives = ARGV[1].to_i
possible_outcomes = 6 ** n_dice
desirable_outcomes = 0
max_digits = ( Math.log(possible_outcomes+1) / Math.log(10) ).ceil

outcome = [1] * n_dice
for i in 0...possible_outcomes
desirable = outcome.select { |n| n == 5 }.length >= min_fives
if mode == "v" || mode == "s" && ( i % 50_000 ).zero?
puts "#{'%*d' % [ max_digits, i+1 ]} [#{outcome.join(',')}]#{desirable ? ' <==' : ''}"
end
desirable_outcomes += 1 if desirable
for die in 0...n_dice
if outcome[die] == 6
outcome[die] = 1
else
outcome[die] += 1
break
end
end
end

puts
puts "Number of desirable outcomes is #{desirable_outcomes}"
puts "Number of possible outcomes is #{possible_outcomes}"
puts
puts "Probability is #{desirable_outcomes.to_f / possible_outcomes}"
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top