-
Notifications
You must be signed in to change notification settings - Fork 0
Randomness
Brandon Jordan edited this page Sep 11, 2024
·
13 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.
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.