Programming in Kotlin - Part 13: Challenge: While | Ray Wenderlich

Practice using while loops on your own, through a hands-on challenge.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4736-programming-in-kotlin/lessons/13

which package should I have to import for random variable ?

You can find Random in java.util.*

Random() by default gives: “Abstract class cannot be instanciated.”- message It can be with a seed provided. I do not know what a seed is, but in your lesson there is nothing about seed.
Also if I import Random from java.util (import java.util.Random), and try to run the code, the message is:"
Error:(2, 13) Kotlin: Packages cannot be imported"
I can run the code is when “val random = Random(seed: 6)”
then "roll = random.nextInt(6), but the result is always the same through “rolls”.
Only way it worked out: “import kotlin.random.Random”, then “random = java.util.Random()”

Which version of Java are you using? If you are using 8, the default constructor for Random creates the seed for you. It’s possible that if you are using a later version of Java, this constructor doesn’t exist. I looked on Oracle’s site at the Java 13 documentation, and it looks the same. Make sure you are not using a Random from another package.