How do you get a instance of `kotlin.random.Random...
# stdlib
k
How do you get a instance of
kotlin.random.Random
? I don't want to use the
Random.Default
since this is in a multithreaded context. The docs (https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.random/-random/index.html) say
To get a seeded instance of random generator use Random function.
But that
Random
function just links back to the same page. Am I missing something?
e
I don’t understand the problem you have with
Random.Default
.
k
I don't want to pay for the multithreaded safety, I need a lot of random numbers on different threads.
e
And why do you think you have to pay for the threadsafety if you use it from different threads?
I’m really curious. In fact, it is backed by thread-local random implementation (on JDK7+) so it does not matter how many threads concurrently use it.
👍 1
k
Ah I see, I thought it was backed by a single Random instance with locks. Where can I find the JVM implementation?
i
See https://github.com/JetBrains/kotlin/blob/v1.3.30/libraries/stdlib/jdk8/src/kotlin/random/jdk8/PlatformThreadLocalRandom.kt A correction: we use
ThreadLocalRandom
on JDK 8+ and if you have kotlin-stdlib-jdk8 in dependencies.
👍 1
k
Got it, thanks!