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

**URL:** <https://www.ruby-forum.com/t/quiz-probable-iterations-141/116032>\
**Category:** Ruby\
**Created:** [October 1, 2007, 7:15pm UTC](https://www.ruby-forum.com/t/quiz-probable-iterations-141/116032 "2007-10-01T19:15:24Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mental](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c77e96/32.png) [@mental](https://www.ruby-forum.com/u/mental)\
**Post date:** [October 1, 2007, 7:15pm UTC](https://www.ruby-forum.com/t/quiz-probable-iterations-141/116032/1 "2007-10-01T19:15:24Z")

</div>

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}”
