Search⌘ K
AI Features

Solution: Generate Lotto Numbers

Explore how to generate six unique lotto numbers within a range using Ruby by employing an until loop, random number generation, and array handling to avoid duplicates.

We'll cover the following...

Solution

Ruby 3.1.2
count = 0
array = []
until count == 6
lotto_number = rand(49)
lotto_number += 1
if array.include?(lotto_number)
# Number already exist"
next
end
array << lotto_number
count += 1
end
puts "Your winning lotto numbers are #{array}, good luck!"

Explanation

  • Line 3: We use the until loop instead of a while loop. This is equivalent ...