Home > Software design >  In Kotlin, can I have two random values with the second one omitting the first random number?
In Kotlin, can I have two random values with the second one omitting the first random number?

Time:11-09

Here is what I am trying to say:

val firstNumbers = (1..69).random()
val secondNumbers = (1..69).random()

I would like the secondNumbers to omit the random number picked in firstNumbers

CodePudding user response:

If you're just generating two numbers, what you could do is lower the upper bound for secondNumbers down to 68, then add 1 if it's greater than or equal to the first number. This will ensure an even distribution:

val firstNumber = (1..69).random()
var secondNumber = (1..68).random()
if (secondNumber >= firstNumber) {
    secondNumber  = 1
}

CodePudding user response:

Another approach could be to find one number in range 1..69, remove that number from the range and find the second one.

val first = (1..69).random()
val second = ((1..69) - first).random()
  • Related