2

In kotlin-stdlib-common is there any source of randomness available out of the box? Whether it's some implementation of standard java.util.Random, kotlin.math.random* or basic current time millis that I can use to create my own random number generator? I can't find any.

If it's not there, how would you get the source of randomness without setting your own platform dependent implementations? This is the only method I need:

expect class Rng {
    fun nextInt(): Int
}

I'm trying to make it platform agnostic.

Alex
  • 7,460
  • 2
  • 40
  • 51

2 Answers2

1

The answer would be: wait for Kotlin 1.3 to get released where the common library will be enriched with classes and methods that can provide the source for random values.

https://kotlinlang.org/docs/reference/whatsnew13.html#multiplatform-random

Alex
  • 7,460
  • 2
  • 40
  • 51
-1

This maybe a post with many links, which may cause the problem of Your answer is in another castle: when is an answer not an answer?, so I try my best to write the link description. And my understanding of Kotlin Multiplatform is Kotlin-Multiplatform = Kotlin-JVM + Kotlin-JS.

I believe that the random number of Kotlin-JVM is provided by java.util.Random, and Math.Random() if it is Kotlin-JS, with these following reasons:

And, java.util.Random is designed as result platform-independent, and also implementation platform-independent, with these reason:

So, I think,

How would you get the source of randomness without setting your own platform dependent implementations?

Maybe a random-enough seed and a random-enough (P)RNG.

Geno Chen
  • 4,916
  • 6
  • 21
  • 39
  • I appreciate your effort but I can't find anything related to a question I've been asking. – Alex Jul 15 '18 at 00:32