JSFiddle - React, Tailwind, and code Playground
by Erik
HTML
Open console
JavaScript
/**
* Creates a pseudo-random value generator. The seed must be an integer.
*/
function Random(seed) {
this._seed = seed % 2147483647;
if (this._seed <= 0) this._seed += 2147483646;
}
/**
* Returns a pseudo-random value between 1 and 2^32 - 2.
*/
Random.prototype.next = function () {
return this._seed = this._seed * 16807 % 2147483647;
};
/**
* Returns a pseudo-random floating point number in range [0, 1).
*/
Random.prototype.nextFloat = function (opt_minOrMax, opt_max) {
// We know that result of next() will be 1 to 2147483646 (inclusive).
return (this.next() - 1) / 2147483646;
};
/**
Calculate for a date which slots could be on and which should be off.
In case the date is today, it makes sure that the next 3 slots are on.
get_availability_slot(date) -> 24h slot mask true/false for on/off
**/
function get_availability_slots(date) {
isToday = new Date().toDateString() === date.toDateString();
// Step 1: calculate for each hour of the day if available or not
day = date.getDate(); //consisten seed for each day
rand = new Random(day);
slot_available = [];
for (i=0; i< 24;i++) {
if (isToday && i >= date.getHours() && i-3 < date.getHours()) // be sure to be avaialble within 3 hours
slot_available[i] = true;
else
slot_available[i] = rand.nextFloat() > 0.2 ? true : false; // in 80% it's on
}
return slot_available;
}
// TEST
date = new Date();
console.log(get_availability_slots(date));