Created
January 22, 2011 20:47
-
-
Save aarongreenlee/791458 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
# Aaron Greenlee | |
# http://aarongreenlee.com/ | |
# Activity 2.1: Warmup 1 | |
MIN_N = 1 | |
MAX_N = 1000000 | |
STOP_GOING = ['no','n'] | |
KEEP_GOING = ['yes','y'] | |
GOOD_GOING_ANSWERS = KEEP_GOING | STOP_GOING | |
GOOD_GOING_ANSWERS_STRING = "'" + GOOD_GOING_ANSWERS.join('", "') + '"' | |
valid = false | |
# | |
# We should greet the user | |
# | |
puts "Hello and welcome to the warmup random generator (WRG)." | |
# | |
# Accept input from the user | |
# | |
begin | |
valid = good_rangeA = good_rangeB = false | |
puts "\nPlease give me a minimum and maximum number, separated by a comma (ex: 1,40) between 1 and 1,000,000." | |
print "What are the min,max numbers? : " | |
input = gets.chomp.split(',') | |
good_format = input.length == 2 | |
if !good_format | |
puts "\nYou need two numbers separated by a comma!" | |
else | |
min_n = input[0].to_i | |
max_n = input[1].to_i | |
good_rangeA = (min_n < max_n) && min_n.between?(MIN_N,MAX_N) | |
good_rangeB = (max_n > min_n) && max_n.between?(MIN_N,MAX_N) | |
puts "\nThe min (#{min_n}) is greater than the max (#{max_n})!" if !good_rangeA | |
puts "\n1The max (#{max_n}) is less than the min (#{min_n})!" if !good_rangeB | |
end | |
valid = good_format && good_rangeA && good_rangeB | |
end while not valid | |
# | |
# How many random numbers should be generated? | |
# | |
valid = false | |
begin | |
print "\n\nHow many random numbers would you like? : " | |
qty = gets.chomp.to_i | |
valid = qty > 0 | |
puts "\nThat makes no sense. I need a number--and a number of at least one. Could be more." if !valid | |
end while not valid | |
# | |
# Generate the numbers | |
# | |
result = [] | |
i = 0 | |
(1..qty).each { | |
i += 1 | |
rnum = rand(max_n - min_n + 1) + min_n | |
result << rnum | |
} | |
final_result = result.sort | |
# | |
# Show the numbers at least once but as often as they like | |
# | |
show_again = true | |
begin | |
puts "Your random numbers are : " + final_result.join('; ') + ";" | |
valid = false | |
begin | |
puts "\nWould you like to see them again? (" + GOOD_GOING_ANSWERS_STRING + ') : ' | |
input = gets.strip.chomp | |
valid = GOOD_GOING_ANSWERS.include? input | |
puts "Allowed answers are " + GOOD_GOING_ANSWERS_STRING + "! Please clarify." if !valid | |
end while not valid | |
show_again = KEEP_GOING.include? input | |
end while show_again | |
puts "\nThanks for your time.\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment