Skip to content

Instantly share code, notes, and snippets.

@abdullahi-usman
Last active February 16, 2020 17:33
Show Gist options
  • Save abdullahi-usman/e7b801c4000ae28e755448db55a17d71 to your computer and use it in GitHub Desktop.
Save abdullahi-usman/e7b801c4000ae28e755448db55a17d71 to your computer and use it in GitHub Desktop.
Generates Random Color String
import java.lang.StringBuilder
import kotlin.random.Random
fun generateRandomColorString(): String {
val numNum = Random.nextInt(1, 6)
val alphaNum = 6 - numNum
val string = StringBuilder()
var i = 0
val alphaArray = arrayOf("A", "B", "C", "D", "E", "F");
while (i < alphaNum){
string.append(alphaArray.random())
++i
}
i = 0
if (numNum > 0) {
loop@ while (true) {
for (j in 1 until 6) {
if (Random.nextBoolean()) {
string.insert(i, "${Random.nextInt(1, 10)}")
++i
}
if (i == numNum)
break@loop
}
}
}
return string.insert(0, '#') .toString()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment