import scala.util.Random
object HexColors {
// Generates a single random color channel (0..255)
def randomChannel(): Int = {
// Random.nextInt(256) returns 0..255
Random.nextInt(256)
}
// Builds a full random HEX color, e.g. "#a3f09c"
def randomHexColor(): String = {
val r: Int = randomChannel()
val g: Int = randomChannel()
val b: Int = randomChannel()
// Format each channel as two-digit hex
f"#$r%02x$g%02x$b%02x"
}
// Generates N unique random HEX colors
def generateRandomUniqueHexColors(count: Int): List[String] = {
// Use a mutable Set for uniqueness
val colors = scala.collection.mutable.Set[String]()
// Keep generating until we have the desired number
while (colors.size < count) {
colors += randomHexColor()
}
colors.toList
}
def main(args: Array[String]): Unit = {
val n: Int = 12
val colors: List[String] = generateRandomUniqueHexColors(n)
println("Generated HEX colors:")
colors.foreach(println)
}
}
/*
run:
Generated HEX colors:
#b5add8
#e12bda
#a1d765
#875e93
#b10d0b
#ecfcd6
#437b16
#ac35fd
#45dfd6
#5a9cc0
#c822f6
#d2e5c2
*/