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 lottery numbers for 6 numbers out of 37 and 1 power number out of 7 in Kotlin

1 Answer

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

    It uses:
        - Kotlin's built‑in shuffled() for uniform random permutation
        - Random.nextInt() for the single power number
        - clean functions and idiomatic Kotlin style
*/

import kotlin.random.Random

/*
    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 shuffled()
        - Take the first `count` numbers

    This guarantees:
        - all numbers are unique
        - uniform randomness
        - no duplicate checks needed
*/
fun pickDistinctNumbers(count: Int, max: Int): List<Int> {
    return (1..max).toList()
        .shuffled()        // Fisher–Yates under the hood
        .take(count)
}

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

fun main() {
    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("Main numbers (6 out of 37): ${mainNumbers.joinToString(" ")}")
    println("Power number (1 out of 7): $powerNumber")
}


/*
run:

Main numbers (6 out of 37): 1 20 21 22 25 35
Power number (1 out of 7): 5

*/

 



answered Jul 28 by avibootz

Related questions

...