I'd suppose there's no way to generate unique ints...
# opensavvy
d
I'd suppose there's no way to generate unique ints in
random
?
c
Do you mean use
random.nextInt
and ensure you always have different values each time it is called within a single test?
If so, something like this should work:
Copy code
private val randomIntCache by prepared { HashSet<Int>() }

suspend fun TestDsl.randomUniqueInt(): Int {
    val random = random()
    val randomIntCache = randomIntCache()

    while (coroutineContext.isActive) {
        val candidate = random.nextInt()
        if (candidate !in randomIntCache) {
             randomIntCache += candidate
             return candidate
        }
    }
}
As always with random values, it is your responsibility that
nextInt
calls are always executed in the exact same order on every execution, otherwise setting the seed won't reproduce the exact same executions
👍🏼 1
👍🏾 1
d
Thanks!