random number in specific range kotlin

To generate a random number within a specific range in Kotlin, you can use the Random class and its nextInt() method. Here are the steps:

  1. Import the Random class from the kotlin.random package:
import kotlin.random.Random
  1. Create an instance of the Random class:
val random = Random()
  1. Define the range for the random number:
val min = 1
val max = 100
  1. Generate the random number using the nextInt() method:
val randomNumber = random.nextInt(min, max + 1)
  • The nextInt() method takes two arguments: the minimum value (inclusive) and the maximum value (exclusive).
  • In this case, we add 1 to the maximum value to include it in the range.

  • Use the randomNumber variable in your code as needed.

That's it! Following these steps will allow you to generate a random number within a specific range using Kotlin.