2

I have earlier generated unique ids by making use of Math.Rando, method or sometimes by using GUID.NewGUID method. This time I am trying to generate a unique numbers which are based on a specific pattern.

for instance 123ABC123 - the length of the number will always remain 9 and it will contain 3 numeric digits followed by three characters and then followed by three more digits.

Sandhurst
  • 227
  • 2
  • 6
  • 10
  • 1
    Do these need to be unique across multiple applications? Only unique within your application? Can these ids be generated from a single point (like for ex. a IdGenerator class's static GetId() method?) – Rohith Oct 02 '10 at 07:35
  • This needs to be unique within the applications and yes they can be generated from a single point – Sandhurst Oct 02 '10 at 07:43
  • "needs to be unique within the applications" is very confusing; are you talking about more than one application? Are they running at the same time? On different machines? – Dour High Arch Oct 04 '10 at 18:40

1 Answers1

2

If I wanted a purely unique ID I would use:

 TimeSpan uniqueID = DateTime.Now.ticks;

Which guarnatees you get a unique ID regardless of when you call it.

Michael Eakins
  • 4,149
  • 3
  • 35
  • 54
  • 1
    what if someone turn back the clock? – kofucii Oct 02 '10 at 13:05
  • datetime.now.ticks utilizes the CMOS clock internal to the PC. Unless your users are going to make major physical changes to the PC's this should be a very safe method of generating a Unique ID – Michael Eakins Oct 02 '10 at 13:19
  • That clock is still the same one that is set by the user. – Tergiver Oct 02 '10 at 15:31
  • You don't understand. The datetime.now.ticks is not based on the clock you see in the system tray. The datetime.now.ticks is derived from the CPU clock internal. – Michael Eakins Oct 02 '10 at 15:52
  • I don't know where you're getting your information. Go look at the source code for DateTime.Ticks.get. It returns InternalTicks.get which returns the same field that all the other portions of time are stored. That field is the number of milliseconds since.. whatever the date is, I can't remember. That value comes from the real-time clock on the motherboard. – Tergiver Oct 02 '10 at 17:52
  • EXACTLY! From the mother board(CPU) clock. – Michael Eakins Oct 02 '10 at 19:40
  • Yes, but that clock is set either with the BIOS UI or with the "Clock Settings" dialog in Windows. It's set by the user. If the CMOS battery dies, your clock resets. That is the same clock as the one displayed in the system tray. – Tergiver Oct 02 '10 at 21:11
  • You cannot change the Bios clock through the Windows interface. – Michael Eakins Oct 03 '10 at 01:00
  • Check out these links, they explain how to change your Bios clock and yet they never mention simply using the windows clock setting (1) http://www.computing.net/answers/windows-xp/change-bios-clock-time/70846.html (2) http://blogs.msdn.com/b/oldnewthing/archive/2004/09/02/224672.aspx (3) http://www.pcbuyerbeware.co.uk/BIOS.htm In fact link (2) states that you haven't been able to manipulate the Bios clock through Windows since 3.0 or so. – Michael Eakins Oct 03 '10 at 01:04