This article explores different ways to generate a random alphanumeric string in Kotlin.

The idea is to randomly choose characters from a selected range of characters and construct a string of the desired length.

To construct a random alphanumeric string, the ASCII range should consist of digits, uppercase, and lowercase characters, as shown below. We can easily extend the solution to include any other ASCII characters that fall within the defined range.

Download Code

 
The code can be shortened with Kotlin ranges:

Download Code

 
You can also use the List constructor, as shown below:

Download Code

That’s all about generating a random alphanumeric string in Kotlin.