Idiomatic way to generate a random alphanumeric string in Kotlin

Solution 1:

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("")
}

Solution 2:

Lazy folks would just do

java.util.UUID.randomUUID().toString()

You can not restrict the character range here, but I guess it's fine in many situations anyway.