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:
parent
2a4d60bed8
commit
ec599c8f8a
2 changed files with 4 additions and 6 deletions
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue