Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,685 questions

55,437 answers

573 users

How to generate random Powerball lottery numbers (pick 5 numbers from 1-69 + 1 Powerball from 1-26) in Ruby

1 Answer

0 votes
#
# Generate random Powerball lottery numbers:
#   - 5 distinct numbers from 1–69
#   - 1 distinct Powerball number from 1–26
#
# This program uses:
#   - Ruby's built‑in Array#sample for unique selections
#   - Ruby's built‑in rand for simple integer generation
#   - clean, idiomatic functions
#

#
# generate_main_numbers:
# Returns 5 UNIQUE numbers from 1..69.
# Array#sample(n) automatically:
#   - prevents duplicates
#   - returns exactly n unique values
#
def generate_main_numbers
  (1..69).to_a.sample(5).sort
end

#
# generate_powerball:
# Returns a single number from 1..26.
# Powerball is drawn from its own pool, independent of main numbers.
#
def generate_powerball
  rand(1..26)
end

#
# Main execution:
# Generate and print the Powerball ticket.
#
main_numbers = generate_main_numbers
powerball    = generate_powerball

puts "Random Powerball numbers:"
puts "Main numbers: #{main_numbers.join(' ')}"
puts "Powerball: #{powerball}"



#
# run:
#
# Random Powerball numbers:
# Main numbers: 4 7 26 48 64
# Powerball: 20
#

 



answered Jul 30 by avibootz

Related questions

...