Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class SeqentialGuidFactory {
- static long s_currentSequentialBase;
- static long s_sequentialGuidCounter;
- static SeqentialGuidFactory() {
- Random rnd = new Random(Guid.NewGuid().ToString().GetHashCode());
- s_currentSequentialBase = rnd.Next();
- }
- public static long CurrentSequentialBase {
- get {
- if(s_currentSequentialBase <= 0)
- s_currentSequentialBase = new Random().Next();
- return s_currentSequentialBase;
- }
- set { s_currentSequentialBase = value; }
- }
- public static Guid Create() {
- var ticksAsBytes = BitConverter.GetBytes(s_currentSequentialBase);
- Array.Reverse(ticksAsBytes);
- var increment = System.Threading.Interlocked.Increment(ref s_sequentialGuidCounter);
- var currentAsBytes = BitConverter.GetBytes(increment);
- Array.Reverse(currentAsBytes);
- var bytes = new byte[16];
- Array.Copy(ticksAsBytes,0,bytes,0,ticksAsBytes.Length);
- Array.Copy(currentAsBytes,0,bytes,8,currentAsBytes.Length);
- return new Guid(bytes);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment