Reputation: 7403
I need help with understanding how to tackle this problem of generating Unique Serial Numbers or UIDs for mass produced products.
I will be producing products in mass, as of now I think that I will be making >50000 units. When these units are manufactured and ready I must test them before dispatching and at which point in time I would also like to give each one of them a Unique ID.
I was thinking of writing an application to generate a 128 bit unique number generator application and ask the testers to run it on their jigs, generate an ID, assign the ID and then carry out testing.
But my question is, please look at the illustration below, if my same application is running on say 3 (or more) different jigs in parallel at the same time, how do I make sure that the numbers generated by these systems are never ever equal to one another?
How do I programmatically make sure that Unique Serial #1 != Unique Serial #2 != Unique Serial #3?
If they are ever equal by any chance I will land in great trouble tracking them, once they are out.
There are some hardware approaches, such as making use of UID chips/EEPROMs from Atmel or Maxim, but they will add cost. I wish to solve this at software level and keep the hardware approach as a last resort.
I have found out that the test jigs are capable of running C# or Python applications.
Please help!
Upvotes: 1
Views: 3138
Reputation: 4797
I was in a similar situation and solved it with the help of centralized "generator", a stored procedure in SQL.
Table structure:
CREATE TABLE [dbo].[UniqueKeys](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[App] [nvarchar](100) NOT NULL,
[System] [nvarchar](100) NOT NULL,
[Key] [bigint] NOT NULL
) ON [PRIMARY]
Here is the code for the SP:
CREATE PROCEDURE [dbo].[GetUniqueKeyTV]
@app VARCHAR(100) ,
@sys VARCHAR(100) ,
@retVal BIGINT OUT
AS
BEGIN
DECLARE @T TABLE([Key] BIGINT);
SET NOCOUNT ON
MERGE INTO dbo.UniqueKeys WITH (TABLOCK) AS Target
USING ( VALUES
( @app ,
@sys ,
1
) ) AS Source ( [App], [System], [Key] )
ON ( Target.App = Source.App
AND Target.[System] = Source.[System]
)
WHEN MATCHED
THEN UPDATE
SET Target.[Key] = Target.[Key] + 1
WHEN NOT MATCHED BY TARGET
THEN
INSERT (
[App] ,
[System] ,
[Key]
) VALUES
( Source.[App] ,
Source.[System] ,
Source.[Key]
) OUTPUT
inserted.[Key] INTO @T;
SELECT @retVal = [Key]
FROM @T
SET NOCOUNT OFF
END
GO
It lets you create sequential numbers for an application and system, but you can modify it to suit a specific need.
Upvotes: 0
Reputation: 36
What you need is a GUID generator. GUIDs composed of network ID and time are considered unique. The potential for duplication is insignificant.
Upvotes: 2