/// <summary> /// Generates a new ID. /// </summary> /// <returns></returns> public long Generate() { long ms = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); long state = Interlocked.Increment(ref this._state) & 0x07; // 3 bits of state using var rng = new SecureRandom(); long differentiator = (rng.GetUInt8() & 0x0F) << 8; // 4 bits of differentiator return((ms << 12) | differentiator | this._pid | state); }