Basic-benchmark-experiment/Kotlin/code_unoptimised.kt

20 lines
553 B
Kotlin
Raw Normal View History

2024-03-11 12:43:52 +02:00
import kotlin.random.Random
fun main(args: Array<String>) {
val totalAttempts: Int = 15_000_000
var successfulAttempts: Int = 0
for (i in 0..totalAttempts) {
var a: Int = Random.nextInt(256)
var b: Int = Random.nextInt(256)
var c: Int = Random.nextInt(256)
if (a + b + c > 300) {
successfulAttempts++
}
}
println("Iterations: $totalAttempts")
println("Valid sums: $successfulAttempts")
println("Probability: ${successfulAttempts.toFloat() / totalAttempts.toFloat()}")
}