...

/

Solution: Generating Lotto Numbers Using a Method

Solution: Generating Lotto Numbers Using a Method

Take a look at the solution to the problem from the previous lesson and see the method call in action.

We'll cover the following...

Solution

Press + to interact
def get_next_valid_lotto_number(existing_lotto_numbers)
new_lotto_number = rand(49)
while existing_lotto_numbers.include?(new_lotto_number)
new_lotto_number = rand(49)
end
return new_lotto_number
end
lotto_numbers = []
6.times do
new_valid_number = get_next_valid_lotto_number(lotto_numbers)
lotto_numbers << new_valid_number
end
puts "Your winning lotto numbers are #{lotto_numbers.inspect}, good luck!"

Explanation

  • Line 2: We generates a ...