tags:

views:

65

answers:

1

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.

+2  A: 

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
what if someone turn back the clock?
kofucii
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
That clock is still the same one that is set by the user.
Tergiver
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
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
EXACTLY! From the mother board(CPU) clock.
Michael Eakins
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
You cannot change the Bios clock through the Windows interface.
Michael Eakins
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