import kotlin.random.Random
// Generates a single random color channel (0..255)
fun randomChannel(): Int {
// Random.nextInt(256) returns 0..255
return Random.nextInt(256)
}
// Builds a full random HEX color, e.g. "#a3f09c"
fun randomHexColor(): String {
val r: Int = randomChannel()
val g: Int = randomChannel()
val b: Int = randomChannel()
// Format each channel as two-digit hex
return "#%02x%02x%02x".format(r, g, b)
}
// Generates N unique random HEX colors
fun generateRandomUniqueHexColors(count: Int): List<String> {
// MutableSet ensures uniqueness automatically
val colors: MutableSet<String> = mutableSetOf()
// Keep generating until we have the desired number
while (colors.size < count) {
colors += randomHexColor()
}
// Convert to a List (immutable)
return colors.toList()
}
fun main() {
val n: Int = 12
val colors: List<String> = generateRandomUniqueHexColors(n)
println("Generated HEX colors:")
colors.forEach { println(it) }
}
/*
run:
Generated HEX colors:
#c75625
#878de6
#039b07
#7d06e7
#d4797e
#7beed9
#f22126
#796922
#99ca11
#7a537e
#4e1fe5
#35e759
*/