gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
kotlin random
- User
kotlin random
- Assistant
In Kotlin, you can generate random numbers using the
Randomclass from thekotlin.randompackage. Here's an example:import kotlin.random.Random fun main() { val random = Random(System.currentTimeMillis()) val randomInt = random.nextInt() // Generate a random integer println("Random integer: $randomInt") val randomDouble = random.nextDouble() // Generate a random double between 0 and 1 println("Random double: $randomDouble") val randomBoolean = random.nextBoolean() // Generate a random boolean println("Random boolean: $randomBoolean") val randomInRange = random.nextInt(100) // Generate a random integer between 0 and 100 println("Random integer in range [0, 100): $randomInRange") }In this example, we first create an instance of
Randomby passing the current system time as the seed. Then, we can use various methods of theRandomclass to generate different types of random values.nextInt()generates a random integer,nextDouble()generates a random double, andnextBoolean()generates a random boolean. You can also use the method with an optional parameter to specify a range for the generated number.