Currently, I am working on a function in Typescript that is supposed to generate a unique number each time it runs. However, there seems to be a problem with the arithmetic as the results are not always correct. Upon further examination of the code below, I suspect that the issue might be related to the behavior of Typescript/Javascript's number type or Math.ceil() function. I would greatly appreciate any insight or advice on how to resolve this.
function createUniqueNumber(){
const millidate = (new Date()).getTime();
const factor:number = 10000;
const codeTime:number = +millidate*factor;
const randomEnd:number = Math.ceil(Math.random()*factor);
const randomMult:number = Math.ceil(Math.random()*10);
const codeBody:number = codeTime+randomEnd;
const uniqueCode:number = codeBody*randomMult;
console.log("Breakdown of " + uniqueCode + ": ", codeTime, randomEnd, codeBody, randomMult);
return uniqueCode;
};
createUniqueNumber();
Here is an image showcasing some tests conducted in Playground:
https://i.sstatic.net/bcdsh.png
I have been using Typescript's Playground for testing purposes, but the performance does not seem to vary significantly across different environments.