All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.data2viz.random.normal.kt Maven / Gradle / Ivy

There is a newer version: 0.8.0-RC5
Show newest version
package io.data2viz.random

import kotlin.math.ln
import kotlin.math.sqrt


fun randomNormal(mu: Number = 0, sigma: Number = 1): () -> Double = {
    var x: Double? = null
    var r = 0.0
    var y = 0.0

    if (x != null) {
        y = x
    } else {
        while (r == 0.0 || r > 1) {
            x = random() * 2 - 1
            y = random() * 2 - 1
            r = x * x + y * y
        }
    }
    mu.toDouble() + sigma.toDouble() * y * sqrt(-2 * ln(r) / r)
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy