0

We are trying to create a random number generator to create serial numbers for products on a virtual assembly line. We got the random numbers to generate, however since they are serial numbers we don't want it to create duplicates. Is there a way that it can go back and check to see if the number generated has already been generated, and then to tell it that if it is a duplicate to generate a new number, and to repeat this process until it has a "unique" number.

Rob_IGS
  • 575
  • 3
  • 8
  • 17
  • Wouldn't it be easier to hash the name/id/something-characteristic of each product? – JCOC611 Apr 05 '11 at 17:59
  • @JCOC611 - hashes will collide so they are not what you want to use here. A deterministic algorithm with guaranteed uniqueness is what you want. – tvanfosson Apr 05 '11 at 18:07

4 Answers4

5

The point of a serial number is that they're NOT random. Serial, by definition, means that something is arranged in a series. Why not just use an incrementing number?

3

The easiest way to fix this problem is to avoid it. Use something that is monotonically increasing (like time) to form part of your serial number. To that you can prepend some fixed value that identifies the line or something.

So your serial number format could be NNNNYYYYMMDDHHMMSS, where NNNN is a 4-digit line number and YYYY is the 4 digit year, MM is a 2 digit month, ...

If you can produce multiple things per second per line, then add date components until you get to the point where only one per unit time is possible -- or simply add the count of items produced this day to the YYYYMMDD component (e.g., NNNNYYYYMMDDCCCCCC).

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • 2
    You can make it shorter by using a higher base for the some of the numbers, say base-32 using the numbers 0-9A-Z (minus ILOQ). That will let you encode the year in 3 "digits", the month and day each in 1 "digit". – tvanfosson Apr 05 '11 at 18:13
0

With a truly random number you would have to store the entire collection and review it for each number. Obviously this would mean that your generation would become slower and slower the larger the number of keys you generate (since it would have to retry more and more often and compare to a larger dataset).

This is entirely why truly random numbers just are never used for this purpose. For serial numbers the standard is always to just do a sequential number - is there any real real for them to be random?

Unique IDs are NEVER random - GUIDs and the like are based on the system time and (most often) MAC address. They're globally unique because of the algorithm used and the machine specifics - not because of the size of the value or any level of randomness.

Personally I would do everything I could to either use a sequential value (perhaps with a unique prefix if you have multiple channels) or, better, use a real GUID for your purpose.

Jim Davis
  • 1,230
  • 6
  • 11
0

is this what you are looking for?

var rArray;

function fillArray (range)
{
    rArray = new Array ();

    for(var x = 0; x < range; x++)
    rArray [x] = x;
}

function randomND (range)
{
    if (rArray == null || rArray.length < 1)
    fillArray (range);

    var pos = Math.floor(Math.random()*rArray.length);
    var ran = rArray [pos];

    for(var x = pos; x < rArray.length; x++)
    rArray [x] = rArray [x+1]; 

    var tempArray = new Array (rArray.length-1)
    for(var x = 0; x < tempArray.length; x++)
    tempArray [x] = rArray [x];

    rArray = tempArray;

    return ran;
}
krayyem
  • 39
  • 1
  • 6