Math.random in kotlin


#1

I don’t find the class Math in the Kotlin reference

When I use Math.random(), Math belongs to Kotlin, Java or JS ??

Thanks for an answer


#2

Math is a part of common library since 1.2 and has separate implementations on JVM and JS. See here for details.


#3

Thanks for your answer, but there’s no random() method in the link you give me

My question is why Math.random () is not found in the Kotlin reference ?


#4

random obliviously is not the part of the package. I do not know about JavaScript, but in JVM it is a bad idea to use this function anyway since generator should be initialized in a controllable fassion. The more or less suitable way is to use Random::nextDouble. For a really good generator you’d better use one of library based generators.


#5

thank you for your quick reply
it’s a Java class (?), so there are no native random tools in Kotlin
Bizarre and pity, but I guess Kotlin is still very young

Have a good day


#6

Good random number generation is a complicated thing. In most cases it could not be done outside specific platform. If you are targeting JavaScript, you probably should use its own generator. If you are building a multi-platform project, you can have separate implementations for different platforms.


#7

The absence of a common random number generator could be a problem when testing multi-platform projects.

Normally, you could set the seed to some constant value to produce reproducible output when testing a JVM application. However, if each platform uses a different generator, this obviously won’t work for multi-platform stuff.

If Kotlin ever gets its own Random class, it would be useful if they could include the option of using a platform-independent ‘naive’ generator for testing purposes. You could then switch to the platform-specific generator for production code.

Incidentally, the kotlin.math package is now formally documented here.

If anyone’s wondering why you can’t just (for example) use ‘abs’ without any qualification, the reason is that the kotlin.math package is not imported automatically. You either need to import all of it:

import kotlin.math.*

or a specific member:

import kotlin.math.abs


#8

A multi platform random number generator can be a 3rd party library.


#9

True, but it would still be nice to have one easily ‘on tap’.


#10

@k.meshoub While we have specific plans about random (KT-17261), note that it’s a non-goal of Kotlin to replicate every other Java tool or API. :wink:

If something is good in JDK, you can just use it. Unless you can’t because you targeting multiple platforms. Is that your case?

@alanfo, thanks for your input about reproducible random sequences, could you describe it in a more detail in the issue above? How do you envision switching to that test random generator?


#11

As we use Math without import, we would think that it is a native class of Kotlin (whereas if I understand correctly it’s a Java class!)

This is confusing, now I don’t come from the Java world, that must be why I am surprised :slight_smile:


#12

Just off the top of my head, you could include a parameter in the Random class constructor which would default to the platform-specific generator but could be set to a platform-independent algorithm or perhaps a choice of such algorithms.

These algorithms would still produce a different sequence of ‘random’ numbers on each run unless you set the seed (another optional constructor parameter and/or a separate property) to some constant value.


#13

Unlike Java, Kotlin allows functions and properties to be declared at the ‘top level’ i.e. outside any class.

Whereas the Java math functions are members of the java.lang.Math class, the Kotlin math functions are ‘top level’ within the kotlin.math package.

Kotlin JVM applications import java.lang.* by default (which is why you can use Math.abs) but not kotlin.math.*. So yes it is a bit confusing but it does avoid conflicts between the two.

There’s a list of the default imports here.


#14

Thank you, it’s much clearer with your answer.

I know now that I must also leave the java.lang doc open when I devellope in Kotlin :man_student:


#15

I am working a lot with random number generators (for Monte-Carlo simulations mostly) and I can say from the experience that sadly there is no ideal generator. Basically you want a thread-safe generator with best possible performance and infinite correlation length. Commons math library has more than ten different generator implementations.

So I do not think it is convenient to have something that complex into standard library. For non-scientific applications it is probably sufficient to have the same functionality that java Random provides. Meaning an ability to set a seed as a single number.


#16

Isn’t the problem though that, if JB were to simply translate the java.util.Random class source code so Kotlin JS and Native could provide identical functionality, they’d likely run into licensing issues? I believe each source file in the OpenJDK is individually licensed.

Even if they wrote something with equivalent functionality from scratch, JVM developers (particularly in mixed language projects) wouldn’t be happy as it wouldn’t produce identical results to java.util.Random.

What I was thinking was that the Kotlin standard library could provide a Random class which normally would just be a wrapper for an appropriate Random class for each platform. However, they could in addition write something fairly simple using a standard algorithm which all platforms could use for testing or even for production if a sophisticated algorithm were not required.

The ability to produce cryptographically strong random numbers is also an issue to consider where there would be similar issues with the JDK’s SecureRandom class. However, as such numbers are non-deterministic by definition regardless of apparent seed, it’s probably best to leave multi-platform developers to decide how best to test them.