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

Create your online store today with Shopify

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

Disclosure: My content contains affiliate links.

43,237 questions

56,140 answers

573 users

How to generate 3 no-digit repeated random integers, each with distinct digits in Kotlin

1 Answer

0 votes
import kotlin.random.Random

// ------------------------------------------------------------
// Function: generate_number
// Purpose: Create one 3-digit integer with distinct digits,
//          drawn from a shared pool of available digits.
//          The first digit is never zero, so the result
//          is always a true 3-digit integer.
// ------------------------------------------------------------
fun generate_number(availableDigits: MutableList<String>, length: Int = 3): Int {

    // Choose the first digit from the pool, excluding '0'
    val nonZeroDigits = availableDigits.filter { it != "0" }
    val firstDigit = nonZeroDigits.random()
    availableDigits.remove(firstDigit)

    // Choose the remaining digits from the updated pool
    val remaining = mutableListOf<String>()
    repeat(length - 1) {
        val d = availableDigits.random()
        remaining.add(d)
        availableDigits.remove(d)
    }

    // Build the number as a string, then convert to int
    val digitsStr = firstDigit + remaining.joinToString("")
    return digitsStr.toInt()
}


// ------------------------------------------------------------
// Function: generate_three_distinct_digit_numbers
// Purpose: Produce three integers, each with distinct digits,
//          and no digit repeated across all three numbers.
//          All numbers are guaranteed to be 3 digits long.
// ------------------------------------------------------------
fun generate_three_distinct_digit_numbers(): Triple<Int, Int, Int> {

    // Start with all digits 0–9 as strings
    val digits = MutableList(10) { it.toString() }

    // Generate three numbers, each 3 digits long
    val n1 = generate_number(digits)
    val n2 = generate_number(digits)
    val n3 = generate_number(digits)

    return Triple(n1, n2, n3)
}


// ------------------------------------------------------------
// Main execution
// ------------------------------------------------------------
fun main() {

    // Run the generator 5 times
    repeat(5) {
        val (n1, n2, n3) = generate_three_distinct_digit_numbers()
        println("($n1, $n2, $n3)")
    }
}



/*
run:

(579, 132, 860)
(709, 312, 684)
(840, 265, 397)
(146, 975, 382)
(751, 849, 603)

*/

 



answered Jul 17 by avibootz

Related questions

...