-
Notifications
You must be signed in to change notification settings - Fork 37
Open
Labels
bugSomething isn't workingSomething isn't working
Description
Operating System
Linux
OPENRNDR version
Recent build from source
ORX version
Recent build from source
Java version (if applicable)
No response
Describe the bug
The uniform() integer extension function in org.openrndr.extra.noise.UniformRandom.kt has an overflow issue in the (max-min) code with some large number ranges that result in non-uniform distribution of values.
Steps to reproduce the bug
Demo of the extreme case:
import kotlin.random.Random
fun main() {
repeat(20) {
println("${Int.uniform(Int.MIN_VALUE, Int.MAX_VALUE)}")
}
println("I'm an overflowing integer: ${(Int.MAX_VALUE - Int.MIN_VALUE)}")
}
fun Int.Companion.uniform(
min: Int = -1, max: Int = 2,
random: Random = Random.Default
) =
(random.nextDouble() * (max - min)).toInt() + minLink to Kotlin playground: https://pl.kotl.in/SQqW-JmZG
It would be interesting to know what the floating point math is trying to achieve over Random.nextInt()
hamoid
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working