Colton Idle
10/23/2023, 3:46 PMVishnu Shrikar
10/23/2023, 3:48 PMColton Idle
10/23/2023, 3:52 PMVishnu Shrikar
10/23/2023, 3:53 PMRandom.nextInt(100)
Francesc
10/23/2023, 4:01 PMRandom.nextInt(100)
gives you an Int in the [0..99] range (both included), it excludes 100. If you want to map 0-10, then you'd have to use Random.nextInt(101)
Vishnu Shrikar
10/23/2023, 4:03 PMCasey Brooks
10/23/2023, 4:05 PMRandom.nextInt(100) < randomWinPercentageAsInt
should do the job. See example here https://pl.kotl.in/ImlV8xKkYFrancesc
10/23/2023, 4:06 PM/**
* Gets the next random non-negative `Int` from the random number generator less than the specified [until] bound.
*
* Generates an `Int` random value uniformly distributed between `0` (inclusive) and the specified [until] bound (exclusive).
*
* @param until must be positive.
*
* @throws IllegalArgumentException if [until] is negative or zero.
*
* @sample samples.random.Randoms.nextIntFromUntil
*/
public open fun nextInt(until: Int): Int = nextInt(0, until)
the upper limit is excludedKlitos Kyriacou
10/23/2023, 4:07 PMnextInt(100)
. You want a value from 0 to 99, and then test it like this:
val won = Random.nextInt(100) < winPercentageRate
If your rate is 50, then Random.nextInt(100) returns a value between 0 and 99. There will be 50 such values between 0 and 49, and 50 such values between 50 and 99, giving an equal chance.Francesc
10/23/2023, 4:09 PMenum class CatchResult {
Win,
Loss,
}
fun winOrLose(val input: Int /* 0-10 */): CatchResult {
val winFraction = input.toDouble() / 10.0
val random = Random.nextInt(101)
val loses = random > (winFraction * 100.0).toInt()
return if (loses) CatchResult.Loss else CatchResult.Win
}
fun winOrLose(input: Int /* 0-10 */): CatchResult {
val random = Random.nextInt(11)
val loses = random > input
return if (loses) CatchResult.Loss else CatchResult.Win
}
Klitos Kyriacou
10/23/2023, 4:15 PMval random = Random.nextInt(11)
val loses = random > inputNo, if input = 10 it should win every time, but if random is 10 it will lose. You really need
nextInt(10)
so between 0 and 9.Francesc
10/23/2023, 4:15 PM>
is always false for input 10val loses = input == 0 || random > input
Vishnu Shrikar
10/23/2023, 4:17 PMKlitos Kyriacou
10/23/2023, 4:19 PMval loses = input == 0 || random > input
Introducing "special cases" is generally suspicious that something is wrong. Here, the probabilities are not evenly distributed.
It should be random.nextInt(10) >= input
as Vishnu said.Colton Idle
10/23/2023, 4:31 PMVishnu Shrikar
10/23/2023, 4:32 PMColton Idle
10/23/2023, 4:38 PM