import scala.util.Random
object DistinctDigitGenerator {
// Generate N random integers, each with distinct digits and exact length L
def generateNumbers(n: Int, length: Int): List[Int] = {
// Helper: checks if all digits in the number are distinct
def hasDistinctDigits(num: Int): Boolean = {
val digits = num.toString
digits.distinct.length == digits.length
}
// Lower and upper bounds for numbers with exactly L digits
val lower = math.pow(10, length - 1).toInt
val upper = math.pow(10, length).toInt - 1
// Keep generating until we collect N valid numbers
def loop(acc: List[Int]): List[Int] = {
if (acc.length >= n) acc
else {
val candidate = Random.between(lower, upper + 1)
if (hasDistinctDigits(candidate))
loop(candidate :: acc)
else
loop(acc)
}
}
loop(Nil)
}
def main(args: Array[String]): Unit = {
val result = generateNumbers(10, 6)
println(result)
}
}
/*
run:
List(463908, 340621, 980126, 735109, 497250, 617320, 362879, 938206, 967421, 281945)
*/