How to Generate Unique Serial Number..
-
For Shopping Cards i want to Generate Unique Serial Number for every card(countless no of cards) what is the solution plz help me
-
For Shopping Cards i want to Generate Unique Serial Number for every card(countless no of cards) what is the solution plz help me
They need to have an order or they must be "random"? They have a size limit? One of the possible solutions is to use GUID. When GUID is not possible, I generally use a long value, that is get with DateTime.Ticks.Now (or incremented randomically) if both calls are in the same microsecond. The class code follows, but you only need to call: TicksOrIncrement.GetValue() to use it.
using System;
namespace Pfz.Threading
{
/// <summary>
/// Class that returns the value of DateTime.Now.Ticks when GetValue is called
/// but also guarantees that two calls will generate different values, even if
/// they are done one just after the other.
/// </summary>
public static class TicksOrIncrement
{
private static readonly object fLock = new object();
private static long fLastValue = DateTime.Now.Ticks;
private static readonly Random fRandom = new Random();/// <summary> /// Gets a new DateTime.Now.Ticks value or a random incremented value if /// this is a call done at the same microsecond of the last one. /// </summary> public static long GetNext() { long ticks = DateTime.Now.Ticks; lock(fLock) { if (ticks <= fLastValue) ticks = fLastValue + 1 + fRandom.Next(1000); fLastValue = ticks; } return ticks; }
}
} -
They need to have an order or they must be "random"? They have a size limit? One of the possible solutions is to use GUID. When GUID is not possible, I generally use a long value, that is get with DateTime.Ticks.Now (or incremented randomically) if both calls are in the same microsecond. The class code follows, but you only need to call: TicksOrIncrement.GetValue() to use it.
using System;
namespace Pfz.Threading
{
/// <summary>
/// Class that returns the value of DateTime.Now.Ticks when GetValue is called
/// but also guarantees that two calls will generate different values, even if
/// they are done one just after the other.
/// </summary>
public static class TicksOrIncrement
{
private static readonly object fLock = new object();
private static long fLastValue = DateTime.Now.Ticks;
private static readonly Random fRandom = new Random();/// <summary> /// Gets a new DateTime.Now.Ticks value or a random incremented value if /// this is a call done at the same microsecond of the last one. /// </summary> public static long GetNext() { long ticks = DateTime.Now.Ticks; lock(fLock) { if (ticks <= fLastValue) ticks = fLastValue + 1 + fRandom.Next(1000); fLastValue = ticks; } return ticks; }
}
}Yes Lenght should b 14 and i want randoms coz in sequence there is risk of pattern matching
-
For Shopping Cards i want to Generate Unique Serial Number for every card(countless no of cards) what is the solution plz help me
-
GUIDs are rather long (128 bits--32 hex digits). If one had a good 40-bit random number generator, one could generate roughly 2^17 (130,000) items without the risk of even a single collision becoming very significant). The output from a 40-bit generator could be converted to a 12-digit number, and possibly merged with a header and/or checksum to yield a 16-digit card number (a common size). BTW, does anyone know whether putting a GUID through an MD5 or similar hash function and grabbing the first few bytes of the result would be a good way to generate 'random' numbers of e.g. 32-96 bits that are not supposed to duplicate more often than purely random numbers would?
-
Yes Lenght should b 14 and i want randoms coz in sequence there is risk of pattern matching
Since you want randomness and no duplicates, do the following. Generate a list of all consecutive numbers m to n (where m is a 15 digit number, and you may want to ignore any number that has duplicate adjacent characters, i.e. 4672950748223546). Set Count to n-m-1. Using any "good" RNG (which still could have duplicates), generate a random number x between 0 and Count, and use x as an index to select a number from the list m to n. Save the selected number in the serial number list, then remove it from the m to n list and decrement Count. Repeat n-m-1 times. Caution: This can be very slow if you have a massive list (the serial number list grows linearly - write each developed number to a file, but shrinking the massive list is slow). If you are interested, I can dig out a fast C implementation with multiple arrays that I used for randomly selecting words from a dictionary (where the list was a list of file offsets to the dictionary words). Dave.