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,943 questions

55,787 answers

573 users

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

1 Answer

0 votes
import scala.util.Random

/*
    Generate random Powerball lottery numbers:
        - 5 distinct numbers from 1–69
        - 1 distinct Powerball number from 1–26

    This program uses:
        - scala.util.Random for RNG
        - Set for uniqueness
        - clean, idiomatic Scala functions
*/

/*
    generateMainNumbers():
    Generates 5 UNIQUE numbers in the range [1, 69].
    Uses a Set to enforce uniqueness.
*/
def generateMainNumbers(rng: Random): Seq[Int] = {
  var chosen: Set[Int] = Set.empty

  // Keep adding until we have 5 distinct numbers
  while (chosen.size < 5) {
    val n: Int = rng.between(1, 70)   // 1–69
    chosen += n
  }

  chosen.toSeq.sorted
}

/*
    generatePowerball():
    Generates a single number in the range [1, 26].
*/
def generatePowerball(rng: Random): Int = {
  rng.between(1, 27)                  // 1–26
}

@main def powerballGenerator(): Unit = {
  val rng: Random = new Random()

  val mainNumbers: Seq[Int] = generateMainNumbers(rng)
  val powerball: Int = generatePowerball(rng)

  println("Random Powerball numbers:")
  println("Main numbers: " + mainNumbers.mkString(" "))
  println("Powerball: " + powerball)
}



/*
run:

Random Powerball numbers:
Main numbers: 41 45 53 54 64
Powerball: 23

*/

 



answered Jul 30 by avibootz

Related questions

...