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,845 questions

55,672 answers

573 users

How to generate a random color in RGB format with Kotlin

2 Answers

0 votes
import kotlin.random.Random

fun generateRandomRGBColor() {
    val red = Random.nextInt(0, 256)
    val green = Random.nextInt(0, 256)
    val blue = Random.nextInt(0, 256)

    println("Random RGB Color: rgb($red, $green, $blue)")
}

fun main() {
    generateRandomRGBColor()
}



/*
run:

Random RGB Color: rgb(143, 3, 178)

*/

 



answered Oct 9, 2025 by avibootz
0 votes
/*
    Generate a random color in RGB format: rgb(R, G, B)
    This program demonstrates how numbers and bits are used
    to produce valid 8‑bit channel values.
*/

import kotlin.random.Random

// Create a random 8‑bit integer (0–255).
// Random.nextInt(256) returns a value in [0, 256),
// giving exactly one byte of color data.
fun randomChannel(): Int {
    // 8 bits → values from 0 to 255
    return Random.nextInt(256)
}

// Produce a random RGB color by combining the channels.
fun generateRandomRGB(): Triple<Int, Int, String> {
    val r = randomChannel()   // Red channel (8 bits)
    val g = randomChannel()   // Green channel (8 bits)
    val b = randomChannel()   // Blue channel (8 bits)

    // Construct the CSS-style RGB string
    val rgb = "rgb($r, $g, $b)"

    return Triple(r, g, rgb)
}

fun main() {
    val (r, g, rgb) = generateRandomRGB()

    println("Red (8 bits): $r")
    println("Green (8 bits): $g")
    println("Blue (8 bits): ${rgb.substringAfterLast(", ").removeSuffix(")")}")
    println("RGB color: $rgb")
}


/*
run:

Red (8 bits): 143
Green (8 bits): 23
Blue (8 bits): 188
RGB color: rgb(143, 23, 188)

*/

 



answered 1 day ago by avibootz
...