~ flowtime algorithm walkthrough ~

node v8.17.0
version: 1.0.0
endpointsharetweet
The purpose of this notebook is to walkthrough the algorithm used to compute the flow time.
First we get the current time
const now = new Date()
Then we create a unique identifiers for the current day and hour
const getSeedsFromDate = (date) => { const year = date.getFullYear() const month = (date.getMonth() + 1).toString().padStart(2, '0') const day = date .getDate() // day of the month .toString() .padStart(2, '0') const hour = date .getHours() .toString() .padStart(2, '0') return { minute: parseInt(`${year}${month}${day}${hour}`), hour: parseInt(`${year}${month}${day}`) } } const seed = getSeedsFromDate(now)
Next we create pseudo-random number generator with the seeds
const createLcg = (seed, modulus, multiplier, increment) => { const m = modulus const a = multiplier const c = increment let z = seed return { rand: () => { z = (a * z + c) % m return z / m } } } const getHourLcg = (seed) => { // values from the Numerical Recipes book return createLcg(seed, 4294967296, 1664525, 1013904223) } const getMinuteLcg = (seed) => { // values from MINSTD return createLcg(seed, 2147483647, 48271, 0) } const hourLcg = getHourLcg(seed.hour) const minuteLcg = getMinuteLcg(seed.minute)
Now we generate the sequence of hours
const getSequenceFromLcg = (lcg, length) => { const values = [] // generate sequence for (let i = 0; i < length; i++) { values.push(lcg.rand()) } // copy and sort sequence const sorted = values.slice().sort() // normalize values based on their order for (let i = 0; i < sorted.length; i++) { values[values.indexOf(sorted[i])] = i } return values } const hourSequence = getSequenceFromLcg(hourLcg, 24)
And the sequence of minutes
const minuteSequence = getSequenceFromLcg(minuteLcg, 60)
Finally we map the date hours and minutes to the sequence we generated to create the flow time
const time = { hour: hourSequence[now.getHours()], minute: minuteSequence[now.getMinutes()], second: now.getSeconds() }
Loading…

no comments

    sign in to comment