#getting-started
Title
# getting-started
c

Colton Idle

10/23/2023, 3:46 PM
I'm working on a "claw game" sort of thing where the randomization is dictated by my backend. The backend returns between a 0 and 10 and this is the percentage of "loser every time" to "winner every time". (0 loser, and 10 winner) and everything else between is just a percent. So ~5 is roughly 50% win/lose rate. I guess it'd be easier if the server used 0-9 instead of 0-10, but yeah. What's the best way to take an "int" and come up with a winPercentageRate based off of that?
v

Vishnu Shrikar

10/23/2023, 3:48 PM
your range is 0-10 so... just divide by 10? Or 10.0 if you want more accuracy... Then multiply by 100 and you have your percentage
c

Colton Idle

10/23/2023, 3:52 PM
Once i have the percentage... is there a stdlib method to get a boolean based on a random probability?
v

Vishnu Shrikar

10/23/2023, 3:53 PM
Copy code
``Random.nextInt(100)``
Use that and just check where the result falls in relation to ur percent
f

Francesc

10/23/2023, 4:01 PM
``Random.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)``
v

Vishnu Shrikar

10/23/2023, 4:03 PM
Yes thats right, whoops, i forgot the range thing
Wait I just read the docs, the ranges are inclusive
So the original 100 would be correct
c

Casey Brooks

10/23/2023, 4:05 PM
Something like
``Random.nextInt(100) < randomWinPercentageAsInt``
should do the job. See example here https://pl.kotl.in/ImlV8xKkY
1
f

Francesc

10/23/2023, 4:06 PM
Copy code
``````/**
* 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 excluded
k

Klitos Kyriacou

10/23/2023, 4:07 PM
You actually do need
``nextInt(100)``
. You want a value from 0 to 99, and then test it like this:
Copy code
``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.
f

Francesc

10/23/2023, 4:09 PM
Copy code
``````enum 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
}``````
you can actually simplify all this
Copy code
``````fun winOrLose(input: Int /* 0-10 */): CatchResult {
val random = Random.nextInt(11)
val loses = random > input
return if (loses) CatchResult.Loss else CatchResult.Win
}``````
k

Klitos Kyriacou

10/23/2023, 4:15 PM
val random = Random.nextInt(11)
val loses = random > input
No, 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.
f

Francesc

10/23/2023, 4:15 PM
random is at most 10, so the
``>``
is always false for input 10
however, it should be
``val loses = input == 0 || random > input``
v

Vishnu Shrikar

10/23/2023, 4:17 PM
i think its random.nextInt(10) >= input
k

Klitos Kyriacou

10/23/2023, 4:19 PM
> random is at most 10, so the > is always false for input 10 You're right, I had got it the wrong way around. If input is 0, it should lose, but if random is 0 it wins. >
Copy code
``val 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.
c

Colton Idle

10/23/2023, 4:31 PM
Glad it wasn't just me that was getting caught up in the details. Maybe i need more coffee. lol. Thank you all! @Casey Brooks your example was amazing since it printed all of the rates and I could verify that things were working as expected. thank you for teaching!
v

Vishnu Shrikar

10/23/2023, 4:32 PM
If you are young remember coffeee stunts your growth 🙂 but sometimes the simplest things are the hardest, 🙂
😅 2
c

Colton Idle

10/23/2023, 4:38 PM
Yeah, I ended up writing something that had like 20 lines of code... and I was like. wait. there has to be a better way. then i started going down the route of looking up a probability stdlib in java/kotlin then i just ended up posting here. Glad that the solution was a one liner.