0
0
KotlinProgramBeginner · 2 min read

Kotlin Program to Generate Random Number

In Kotlin, you can generate a random number using val randomNumber = kotlin.random.Random.nextInt() for any integer or Random.nextInt(from, until) for a range.
📋

Examples

InputNo input needed
OutputRandom number like 123456789
InputRandom.nextInt(1, 10)
OutputRandom number between 1 and 9, e.g., 7
InputRandom.nextInt(0, 1)
OutputAlways 0 (since upper bound is exclusive)
🧠

How to Think About It

To generate a random number in Kotlin, think of using the built-in Random class. You can get a random integer either from the full integer range or specify a range by giving a start and end. The Random class handles the complexity of randomness for you.
📐

Algorithm

1
Import the kotlin.random.Random class.
2
Call Random.nextInt() to get a random integer.
3
Optionally, provide a range with two numbers to get a random number within that range.
4
Print or use the generated random number.
💻

Code

kotlin
import kotlin.random.Random

fun main() {
    val randomNumber = Random.nextInt(1, 101) // random number from 1 to 100
    println("Random number between 1 and 100: $randomNumber")
}
Output
Random number between 1 and 100: 57
🔍

Dry Run

Let's trace generating a random number between 1 and 100 through the code

1

Call Random.nextInt(1, 101)

Generates a random integer starting at 1 up to but not including 101

2

Store the result in randomNumber

randomNumber = 57 (example value)

3

Print the randomNumber

Output: Random number between 1 and 100: 57

SteprandomNumber
157
💡

Why This Works

Step 1: Using Random.nextInt()

The Random.nextInt() function generates a random integer. If you provide two arguments, it generates a number from the first argument (inclusive) to the second (exclusive).

Step 2: Specifying a range

By giving 1 and 101, you get numbers from 1 to 100 because the upper bound is exclusive.

Step 3: Printing the result

The println statement shows the random number so you can see the output.

🔄

Alternative Approaches

Using java.util.Random
kotlin
import java.util.Random

fun main() {
    val random = Random()
    val randomNumber = random.nextInt(100) + 1 // 1 to 100
    println("Random number between 1 and 100: $randomNumber")
}
This uses Java's Random class, which is older but still works; Kotlin's Random is preferred for idiomatic code.
Using ThreadLocalRandom
kotlin
import java.util.concurrent.ThreadLocalRandom

fun main() {
    val randomNumber = ThreadLocalRandom.current().nextInt(1, 101)
    println("Random number between 1 and 100: $randomNumber")
}
ThreadLocalRandom is efficient for multi-threaded environments but less common in simple Kotlin programs.

Complexity: O(1) time, O(1) space

Time Complexity

Generating a random number is a constant time operation because it does not depend on input size.

Space Complexity

Only a single integer variable is used to store the random number, so space is constant.

Which Approach is Fastest?

Kotlin's built-in Random is optimized and simple; Java's Random and ThreadLocalRandom add overhead but are useful in specific contexts.

ApproachTimeSpaceBest For
Kotlin Random.nextInt()O(1)O(1)Simple and idiomatic Kotlin code
Java Random.nextInt()O(1)O(1)Legacy Java compatibility
ThreadLocalRandom.nextInt()O(1)O(1)Multi-threaded environments
💡
Use Random.nextInt(from, until) to get a random number within a specific range easily.
⚠️
Beginners often forget that the upper bound in Random.nextInt(from, until) is exclusive, causing off-by-one errors.