Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

First app - Random.nextInt() unresolved reference

I have followed this tutorial for building an android app. https://www.youtube.com/watch?v=EOfCEhWq8sg

It goes well up until the end, where we generate a random val using Random.nextInt(). In Android Studio (3.3), I get an error :

an unresolved reference to nextInt.

I have made sure I am using the right spelling. This program uses Kotlin, and I even imported java.util.* to get the Random library imported.

Please help me out, it is very discouraging not being able to get such a simple application built.

Thanks.

like image 628
Casey Avatar asked Jul 27 '26 17:07

Casey


1 Answers

If you use Random class from java.util package you need to create an instance of Random class. Instances in Kotlin are created using NameOfClass() syntax:

java.util.Random().nextInt()

If you use Random class from kotlin.random package you don't need to create an instance of the class:

kotlin.random.Random.nextInt()
like image 147
Sergey Avatar answered Jul 29 '26 06:07

Sergey