DEV Community

leminhduc1202
leminhduc1202

Posted on

Answer: Idiomatic way to generate a random alphanumeric string in Kotlin

Since Kotlin 1.3 you can do this:

fun getRandomString(length: Int) : String {
    val allowedChars = ('A'..'Z') + ('a'..'z') + ('0'..'9')
    return (1..length)
        .map { allowedChars.random() }
        .joinToString("")
}

Top comments (0)