Replace java.util.Random with kotlin Random object (#4364)

This also improves randomness by avoiding to reinitialize the random
number generator repeatedly from a seed based on the current time.
Typically, if the number generator is reinitialized repeatedly at
non-random times (like multiple times in a row), then generated numbers
have a higher chance of repeating.

The Kotlin Random object is only initialized once, using the best seed
available for the current Android version.
This commit is contained in:
Christophe Beyls 2024-04-10 21:47:27 +02:00 committed by GitHub
commit ec599c8f8a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 4 additions and 6 deletions

View file

@ -3,15 +3,14 @@
package com.keylesspalace.tusky.util
import android.text.Spanned
import java.util.Random
import kotlin.random.Random
private const val POSSIBLE_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
fun randomAlphanumericString(count: Int): String {
val chars = CharArray(count)
val random = Random()
for (i in 0 until count) {
chars[i] = POSSIBLE_CHARS[random.nextInt(POSSIBLE_CHARS.length)]
chars[i] = POSSIBLE_CHARS[Random.nextInt(POSSIBLE_CHARS.length)]
}
return String(chars)
}