1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #!/usr/local/bin/ruby -wKU # encoding: utf-8 class Dice attr_reader :upside def initialize @upside = rand(6) + 1 end def slow srand @upside = rand(6) + 1
end
end h = Hash.new(0) d = Dice.new 100.times do d.slow num = d.upside h[num] += 1 print "#{num} "
end puts puts h h.sort{ |a,b| b[1] <=> a[1] }.each_with_index { |(k,v), i| puts "#{i+1}: Key => #{k} and Value => #{v}." } |
Wednesday, June 04, 2008
Ruby Dice
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment