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

55,473 answers

573 users

How to generate random lottery numbers for 6 numbers out of 37 and 1 power number out of 7 in Scala

1 Answer

0 votes
import scala.util.Random

/*
    This program generates random lottery numbers for:
        - 6 distinct numbers out of 37
        - 1 distinct number out of 7 (power number)

    It uses:
        - scala.util.Random for random number generation
        - Random.shuffle for efficient unique selection
        - clean functions and idiomatic Scala style
*/

/*
    pickDistinctNumbers(count, max):
    Returns `count` distinct random numbers from the range 1..max.

    Algorithm:
        - Build a List of all numbers 1..max
        - Shuffle the list using Random.shuffle
        - Take the first `count` numbers

    This guarantees:
        - all numbers are unique
        - uniform randomness
        - no duplicate checks needed
*/
def pickDistinctNumbers(count: Int, max: Int): List[Int] = {
  val numbers = (1 to max).toList
  Random.shuffle(numbers).take(count)
}

/*
    pickPowerNumber(max):
    Returns a single random number in the range 1..max.
*/
def pickPowerNumber(max: Int): Int = {
  Random.nextInt(max) + 1
}

@main def LotteryGenerator(): Unit = {
  val mainCount = 6
  val mainMax   = 37
  val powerMax  = 7

  // Generate main numbers (distinct)
  val mainNumbers = pickDistinctNumbers(mainCount, mainMax).sorted

  // Generate power number
  val powerNumber = pickPowerNumber(powerMax)

  // Output results
  println(s"Main numbers (6 out of 37): ${mainNumbers.mkString(" ")}")
  println(s"Power number (1 out of 7): $powerNumber")
}


/*
run:

Main numbers (6 out of 37): 6 15 21 24 26 37
Power number (1 out of 7): 5

*/

 



answered Jul 28 by avibootz

Related questions

...