Consider following code,
// Code from https://stackoverflow.com/a/66035934/14659574
const getRandomTime = (offsetHour = 0, rangeInHours = 2) => {
const curTicks = Date.now() + 0 * (offsetHour*60*60*1000);
const addTicks = (Math.random()*rangeInHours)*60*60*1000;
return new Date(curTicks + addTicks);
};
console.log((new Date()));
for(let i=0; i<10;i++) {
console.log( getRandomTime(1, 2+i));
}
It doesn't respect the range instead it returns random time exceeding the range. I want a random time within a range.
For example, Let's say I want a random time that's 2 hours from now but this function gives me values like 5 hours from now.
The code is taken from my previous question. It's looks like the author of the answer is inactive so I decided to make a follow up question.
I have dyscalculia and unable to debug this. I even tried to label the numbers but it not going anywhere.
What's the wrong with this code?