Implement a skewed bell random for the added time in Random Hour

This commit is contained in:
Danilo Titato
2023-03-01 21:32:36 -03:00
parent 4b38bf62b1
commit 8180cb482b
5 changed files with 44 additions and 28 deletions
+18 -3
View File
@@ -6,9 +6,24 @@ const randomInRange = (min, max) => {
return Math.floor(Math.random() * (max - min)) + min;
};
const randomInRangeNoRounding = (min, max) => {
return Math.random() * (max - min) + min;
};
const randomBellSkew = (min, max, skew) => {
let u = 0, v = 0;
while(u === 0) u = Math.random() //Converting [0,1) to (0,1)
while(v === 0) v = Math.random()
let num = Math.sqrt( -2.0 * Math.log( u ) ) * Math.cos( 2.0 * Math.PI * v )
num = num / 10.0 + 0.5 // Translate to 0 -> 1
if (num > 1 || num < 0)
num = randomBellSkew(min, max, skew) // resample between 0 and 1 if out of range
else{
num = Math.pow(num, skew) // Skew
num *= max - min // Stretch to fill range
num += min // offset to min
}
return num
}
const logMessage = (module, message) => {
console.log(`[${module}]: ${message}`);