Last active
February 16, 2020 17:33
-
-
Save abdullahi-usman/e7b801c4000ae28e755448db55a17d71 to your computer and use it in GitHub Desktop.
Generates Random Color String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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