Skip to content

Instantly share code, notes, and snippets.

@NeMO84
Created January 21, 2011 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NeMO84/789317 to your computer and use it in GitHub Desktop.
Save NeMO84/789317 to your computer and use it in GitHub Desktop.
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