From 74dfca4e2e265a17bcba9b4d968a56ddd5d07ad9 Mon Sep 17 00:00:00 2001 From: Roland Grinis Date: Wed, 7 Apr 2021 15:11:00 +0100 Subject: [PATCH] Using kmath-stat for random normals --- .../kotlin/space/kscience/kmath/tensors/core/utils.kt | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/utils.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/utils.kt index 785b59ede..c99f9d156 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/utils.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/utils.kt @@ -1,8 +1,8 @@ package space.kscience.kmath.tensors.core +import space.kscience.kmath.samplers.GaussianSampler +import space.kscience.kmath.stat.RandomGenerator import space.kscience.kmath.structures.* -import kotlin.random.Random -import kotlin.math.* /** * Returns a reference to [IntArray] containing all of the elements of this [Buffer]. @@ -37,8 +37,9 @@ internal fun Buffer.array(): DoubleArray = when (this) { } internal inline fun getRandomNormals(n: Int, seed: Long): DoubleArray { - val u = Random(seed) - return (0 until n).map { sqrt(-2.0 * ln(u.nextDouble())) * cos(2.0 * PI * u.nextDouble()) }.toDoubleArray() + val distribution = GaussianSampler(0.0, 1.0) + val generator = RandomGenerator.default(seed) + return distribution.sample(generator).nextBufferBlocking(n).toDoubleArray() } internal inline fun minusIndexFrom(n: Int, i: Int) : Int = if (i >= 0) i else {