With size-limited data types, there must eventually be duplicates, as they can only have a limited number of different states (e.g. 2^32 for the typical 32-bit integer).
In your case, it's even more limited:
(i % 1000000)
can have one million different values (if we allow negative i
values and the typical interpretation of the %
operator, it's nearly two millions, 1999999 values, but I assume i
will always be positive).
- Then (i % 1000000) * 1000 can also have exactly one million different values.
ms
can have one thousand different values.
- The sum
(i % 1000000) * 1000 + ms
combines one million cases with one thousand cases, giving one billion different cases.
So, at least after one billion invocations, there must be a duplicate.
But there's the peculiar combination of a counter i
with a real-time milliseconds value.
If you systematically increment i
by one with every invocation, the earliest possible duplicate comes after one million invocations.
Depending on the time span for a million invocations, it's highly unlikely that you get one billion different values before the first duplicate. I'd expect something between one and two million invocations, unless you reach multi-million invocations per second.
If you want to make the best duplicates-free use of the range 0...999999999, simply use:
i % 1000000000
Then you're sure that you have the first duplicate exactly after one billion invocations.
f(i=x, ms) = f(i=x+n*1000000, ms)
i<1000000
, does not happen. Ifi>=1000000
, 100% guaranteed.i
is an always increasing number". Increasing by how much? Without specifying that the first 6 digits can behave wildly different. Everything from some never changing 6 digit number to completely random 6 digit numbers. Why? Because "always increasing" can be satisfied by digits we never see. Doesn't change 1000000001 evaluations forcing a duplicate but it impacts the probability of previous evaluations.