Sven Obser
06/23/2022, 3:09 PMkotlin.random.Random
produces the same “random” values with each app start on Android. 😨 Is this expected behavior?
Log.e { "java.util.Random().nextInt(): ${java.util.Random().nextInt()}" }
Log.e { "kotlin.random.Random.nextInt(): ${kotlin.random.Random.nextInt()}" }
Output:
java.util.Random().nextInt(): -1853486751
kotlin.random.Random.nextInt(): -351317653
java.util.Random().nextInt(): 1794921748
kotlin.random.Random.nextInt(): -351317653
java.util.Random().nextInt(): 1849140367
kotlin.random.Random.nextInt(): -351317653
ephemient
06/23/2022, 3:17 PMephemient
06/23/2022, 3:18 PMSven Obser
06/23/2022, 3:30 PMval random = java.util.Random().asKotlinRandom()
and supply it to the caller (e.g. listOf().shuffle(random)
)!?kevindmoore
06/23/2022, 3:33 PMephemient
06/23/2022, 3:34 PMThreadLocalRandom.current()
is supposed to be pre-seeded. the issue is that it was pre-seeded upon initialization in zygote, and every app gets a clone of the same instanceSven Obser
06/23/2022, 3:36 PMjava.util.Random()
gives a a Random with a Random seed.ephemient
06/23/2022, 4:22 PMThreadLocal<java.util.Random>
to implement kotlin.random.Random.Default
, e.g. with
dependencies {
modules {
module(kotlin("stdlib-jdk8")) {
replacedBy(kotlin("stdlib-jdk7"), "<https://issuetracker.google.com/issues/234631055>")
}
}
}
in your build.gradle.kts
. but that's a pretty terrible hack that could break other dependencies (as it also means dropping regex capture groups and java stream/time extensions) and won't work long-term (Kotlin is phasing out pre-Java 8 support, https://youtrack.jetbrains.com/issue/KT-45165)ephemient
06/23/2022, 6:43 PMSven Obser
06/24/2022, 7:52 AM