Skip to content

[orx-noise] Non-random outputs with integer uniform() extension function #370

@nfletton

Description

@nfletton

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() + min

Link 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()

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions