Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Algorithms
  4. How to Generate Unique Serial Number..

How to Generate Unique Serial Number..

Scheduled Pinned Locked Moved Algorithms
helptutorialquestion
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shaina2231
    wrote on last edited by
    #1

    For Shopping Cards i want to Generate Unique Serial Number for every card(countless no of cards) what is the solution plz help me

    P R 2 Replies Last reply
    0
    • S shaina2231

      For Shopping Cards i want to Generate Unique Serial Number for every card(countless no of cards) what is the solution plz help me

      P Offline
      P Offline
      Paulo Zemek
      wrote on last edited by
      #2

      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;
      }
      

      }
      }

      S 1 Reply Last reply
      0
      • P Paulo Zemek

        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;
        }
        

        }
        }

        S Offline
        S Offline
        shaina2231
        wrote on last edited by
        #3

        Yes Lenght should b 14 and i want randoms coz in sequence there is risk of pattern matching

        M 1 Reply Last reply
        0
        • S shaina2231

          For Shopping Cards i want to Generate Unique Serial Number for every card(countless no of cards) what is the solution plz help me

          R Offline
          R Offline
          Robin_Roy
          wrote on last edited by
          #4

          Why dont you use GUID...

          S 1 Reply Last reply
          0
          • R Robin_Roy

            Why dont you use GUID...

            S Offline
            S Offline
            supercat9
            wrote on last edited by
            #5

            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?

            1 Reply Last reply
            0
            • S shaina2231

              Yes Lenght should b 14 and i want randoms coz in sequence there is risk of pattern matching

              M Offline
              M Offline
              Member 4194593
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups