import kotlin.random.Random
fun randomExcluding(N: Int, excluded: Set<Int>): Int {
// If all numbers are excluded, return -1
if (excluded.size > N + 1) {
return -1
}
var num: Int
do {
num = Random.nextInt(0, N + 1) // generates [0, N]
} while (num in excluded)
return num
}
fun main() {
val excluded = setOf(2, 5, 7)
val N = 14
val result = randomExcluding(N, excluded)
println(result)
}
/*
run:
13
*/