Created
January 21, 2011 06:19
-
-
Save NeMO84/789317 to your computer and use it in GitHub Desktop.
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
def numeric?(object) | |
true if Float(object) rescue false | |
end | |
def get_user_response(message, validate_is_number = true) | |
valid = false | |
input = nil | |
loop do | |
puts message | |
input = gets.chomp | |
if validate_is_number | |
break if numeric?(input) | |
puts "Invalid Input. Try again." | |
else | |
break | |
end | |
end | |
input | |
end | |
def rand_between(min,max) | |
Random.new.rand(min..max) | |
end | |
loop do | |
min = 0 | |
max = 0 | |
min = get_user_response("Please enter a minimum number:").to_i | |
while max < min | |
max = get_user_response("Please enter a maximum number:").to_i | |
puts "Maximum number must be greater than #{min}. Try again!" if max <= min | |
end | |
puts "Your random number is #{rand_between(min,max)}.\n" | |
break unless ["y","yes","si","yup","why not!","sure"].include?(get_user_response("Find another random number?", false)) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment