Skip to content

Commit

Permalink
[orx-noise] Add unseeded method to Random (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardomatias authored Feb 27, 2021
1 parent dfd99cd commit 8521ceb
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion orx-noise/src/main/kotlin/Random.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ package org.openrndr.extra.noise
import org.openrndr.math.Vector2
import org.openrndr.math.Vector3
import org.openrndr.math.Vector4
import org.openrndr.shape.Rectangle
import kotlin.math.ln
import kotlin.math.max
import kotlin.math.pow
import kotlin.math.sqrt
import org.openrndr.extra.noise.fbm as orxFbm
import kotlin.random.Random as DefaultRandom


/**
* Deterministic Random using a seed to guarantee the same random values between iterations
*/
object Random {
var rnd: DefaultRandom

Expand Down Expand Up @@ -54,6 +57,30 @@ object Random {
seed = "${seedBase}-${seedTracking}"
}

/**
* Use this when you want to get non-deterministic values aka random every call
*
* @param fn
*/
fun unseeded(fn: Random.() -> Unit) {
val state = rnd
val currentSeed = seed

rnd = DefaultRandom

this.fn()

resetState()

seed = currentSeed
rnd = state
}

/**
* Use this inside `extend` methods to get the same result between iterations
*
* @param fn
*/
fun isolated(fn: Random.() -> Unit) {
val state = rnd
val currentSeed = seed
Expand Down Expand Up @@ -334,5 +361,9 @@ object Random {

return elements.elementAtOrNull(index) ?: elements.last()
}

fun point(rect: Rectangle): Vector2 {
return rect.position(vector2(0.0, 1.0))
}
}

0 comments on commit 8521ceb

Please sign in to comment.