I need to get unique random number in javascript (or Typescript).
At this moment I use this code:
var id = -((new Date()).getTime() & 0xffff);
It returns me numbers like -13915 or -28806 ...
It works most of the time but I am having problems when this code is executed in promises (so nearly multiple times at the same time). Then sometimes I got two identical id.
Is there any solution to get unique random numbers in any case ?
Math.random()(which is probably time-seeded too, but it's way better than your approach)Math.random()is not guaranteed to give you unique numbers. I really don't know why this needs to be repeated so many times...