-
Couldn't load subscription status.
- Fork 0
Randomness
Brandon Jordan edited this page May 1, 2025
·
19 revisions
spark.randomInt(min, max);Maybe? Possibly? Ever have something you just want to happen randomly?
if(spark.maybe()) { // Returns a random boolean.
// ...
}Return a random value from a set of values or an array.
spark.randomValue(1,2,3);
spark.randomValue(...[1,2,3]);Generates a random alphanumeric hash.
spark.hash(length = 6);Create a random ID string with a prefix and appended alphanumeric hash.
spark.randomID(prefix = 'id-', length = 6) // e.g. "id-2wvRjo"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.
let currentValue = x.value;
x.min = 5;
x.max = 10;A RandomStore is an extension of Store with an internal instance of RNG called rng.
const randomY = new RandomStore(0,0,window.innerHeight);
randomY.rng.min = 5;
randomY.rng.max = 10;
// Subscribe to random value.
randomY.model(() => {
// ...
});
randomY.new(); // Subscriber gets a new random value based on min and max.