Created
December 2, 2008 06:57
-
-
Save vikhyat/31034 to your computer and use it in GitHub Desktop.
Roll 2 dice several times (v2)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Rolling a dice is equivalent to generating a random number from 1 to 6. | |
def dice_roll | |
sum = 0 | |
2.times { | |
sum += 1 + rand(6) | |
} | |
sum | |
end | |
puts "How many dice rolls?" | |
num_dr = gets.to_i | |
# <horrible code, need to replace this> | |
# The tally table that will be outputted. Minimum value when 2 die are rolled is 2, maximum is 12 | |
tally = {} | |
tally[2] = 0 | |
tally[3] = 0 | |
tally[4] = 0 | |
tally[5] = 0 | |
tally[6] = 0 | |
tally[7] = 0 | |
tally[8] = 0 | |
tally[9] = 0 | |
tally[10] = 0 | |
tally[11] = 0 | |
tally[12] = 0 | |
# </horrible code, need to replace this> | |
# Before we start, let's record the time. | |
start = Time.now | |
num_dr.times { | |
result = dice_roll | |
tally[result] += 1 | |
} | |
# More code to replace after this. | |
puts '2: ' + tally[2].to_s | |
puts '3: ' + tally[3].to_s | |
puts '4: ' + tally[4].to_s | |
puts '5: ' + tally[5].to_s | |
puts '6: ' + tally[6].to_s | |
puts '7: ' + tally[7].to_s | |
puts '8: ' + tally[8].to_s | |
puts '9: ' + tally[9].to_s | |
puts '10: ' + tally[10].to_s | |
puts '11: ' + tally[11].to_s | |
puts '12: ' + tally[12].to_s | |
# /replace horrible code | |
# Lets check how much time it took as well | |
time_taken = (Time.now - start) | |
puts "Time taken: " + time_taken.to_s + " seconds." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment