From a2e8ed00329421a9a2997c87daa6b28c55ef9edc Mon Sep 17 00:00:00 2001 From: Mads Ager Date: Fri, 10 Jun 2022 09:07:08 +0200 Subject: [PATCH] Do not use ThreadLocalRandom on Android until upcoming SDK 34. On Android devices starting SDK 25, ThreadLocalRandom accidentally started out from the same seed on all application starts. This means that the same sequence of numbers is returned whenever the application is started. This will be fixed in SDK 34. We should avoid using ThreadLocalRandom until then. ^KT-52618 Fixed --- .../kotlin/internal/jdk8/JDK8PlatformImplementations.kt | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/libraries/stdlib/jdk8/src/kotlin/internal/jdk8/JDK8PlatformImplementations.kt b/libraries/stdlib/jdk8/src/kotlin/internal/jdk8/JDK8PlatformImplementations.kt index 2c35969ce54..a004c330871 100644 --- a/libraries/stdlib/jdk8/src/kotlin/internal/jdk8/JDK8PlatformImplementations.kt +++ b/libraries/stdlib/jdk8/src/kotlin/internal/jdk8/JDK8PlatformImplementations.kt @@ -51,7 +51,10 @@ internal open class JDK8PlatformImplementations : JDK7PlatformImplementations() override fun defaultPlatformRandom(): Random = // while ThreadLocalRandom is available since SDK 21 (as documented), it has bugs in the implementation, - // so we don't use it for the same reasons as why we don't use it in JDK7. - if (sdkIsNullOrAtLeast(24)) PlatformThreadLocalRandom() else super.defaultPlatformRandom() + // so we don't use it for the same reasons as why we don't use it in JDK7. ThreadLocalRandom worked on + // SDK 24, but starting SDK 25 it had bugs in seeding so that it would return the same sequence of values + // for all application starts. That will be fixed in SDK 34. Therefore, do not use ThreadLocalRandom until + // then. + if (sdkIsNullOrAtLeast(34)) PlatformThreadLocalRandom() else super.defaultPlatformRandom() }