Skip to content

Randomness

Brandon Jordan edited this page Sep 11, 2024 · 13 revisions

Random Number

spark.randomInt(min, max);

Random Boolean

Maybe? Possibly? Ever have something you just want to happen randomly?

if(spark.maybe()) { // Returns a random boolean.
    // ...
}

Random Value

Return a random value from a set of values or an array.

spark.randomValue(1,2,3);
spark.randomValue(...[1,2,3]);

Random Hash

Generates a random alphanumeric hash.

spark.hash(length = 6);

Random ID

Create a random ID string with a prefix and appended alphanumeric hash.

spark.randomID(prefix = 'id-', length = 6) // e.g. "id-2wvRjo"

Random Number Generator

Spark provides an RNG class for random number generation using the same min and max.

new RNG(init, min, max);
const x = new RNG(0, 0, window.innerWidth);
x.new(); // Creates a new random value and returns it.

Random Store

A RandomStore is an extension of Store and RNG* (*just borrows the new() method).

const randomY = new RandomStore(0,0,window.innerHeight);

// Subscribe to random value.
randomY.model(() => {
    // ...
});

randomY.new(); // Subscriber gets a new random value based on min and max.
Clone this wiki locally