Int Range. random function not compiling

Using the above function random - Kotlin Programming Language

in this code
a10 = (0…9).random() + 48

on compilation get
e: src/main/kotlin/Test.kt: (36, 27): Unresolved reference: random

But I though this was part of the standard library so what am I missing?

It works for me.

package test

fun main() {
    val a10 = (0..9).random() + 48
    println(a10)
}

What version of kotlin are you using? This function is available since 1.3. So if you are using an earlier version of kotlin you need to upgrade.

$ kotlinc -version
info: kotlinc-jvm 1.3.41 (JRE 1.8.0_222-8u222-b10-1ubuntu1~18.04.1-b10)

I am building via gradle
$ gradle -version


Gradle 5.5.1

Build time: 2019-07-10 20:38:12 UTC
Revision: 3245f748c7061472da4dc184991919810f7935a5

Kotlin: 1.3.31
Groovy: 2.5.4
Ant: Apache Ant™ version 1.9.14 compiled on March 12 2019
JVM: 1.8.0_222 (Private Build 25.222-b10)
OS: Linux 4.15.0-65-generic amd64

And that is the problem
buildscript {
ext.kotlin_version = ‘1.2.61’
repositories {
mavenCentral()
}
dependencies {
classpath “org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version”
}
}

Note that the range operator is two dots: .. — the first post seems to contain three.

Here’s your problem. Change to 1.3.50. Also you don’t need kotlinc, Gradle uses compiler bundled into kotlin plugin. It just confuses you. Kotlinc is used to compile files “by hand” and you really should not do that, if you’re not developing your own build tool.

1 Like