Click here to Skip to main content
15,888,351 members
Articles / General Programming
Tip/Trick

Random Unique Code Generator

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
9 May 2013CPOL3 min read 11.3K   7  
Creation of random and unique code

Introduction

I was given a scenario in which I needed to represent a large number by few characters and it must be hard to break the code.

For example: An 8 digit number into 5-6 characters. That too uniquely identified per number (i.e., code corresponding to each number should not match the code corresponding to a different number).

Solution

How do we take a binary form of 78542.

We keep dividing the number by 2 and noting down the remainders like the binary form of 78542 will be 11001011001110.
This is just like representing 78542 to 0 and 1 character representation.

This is called base 2 representation (78542)<sub>10 </sub>= (1001011001110)<sub>2</sub>

Now, what if we follow the same process by changing few things.

Changes

  1. Previously we are dividing it with 2, now we make 38

  2. Change the representing character from only 0 and 1 to “abcdefghijklmnopqrstuvwxyz0123456789$%” (i.e., if the remainder is 1, then we use character “a”, for 2 we use “b” and so on ).

so the representation that we get for number 78542 is (n57v)<sub>38 </sub>by following the above procedure.

So we are done with the changes for representing into less characters.
Note: The number of characters which is used to represent 78542 is 4. If We apply Permutation to find out the range of this character is

38 X 38 X
 38 X 38 = 2085136
. (i.e., we can represent any number between 0 to 2085136 uniquely .

Till now, we are representing all the remainders with a single set of characters (i.e., “abcdefghijklmnopqrstuvwxyz0123456789$%”)

If we use a different set of characters for different places of remainders, like for the first remainder we use “abcdefghijklmnopqrstuvwxyz0123456789$%”, for the second one we use “abcdefghijkl$&#mnopqrstuvwxyz5863190427”, for the third one we use “$nt#07sbpfgh91widxqrze&yvaj683klumco245”, for the fourth one “076$83swint#dxqrze&yvajklumcobpfgh91245”.

Then the representation for 78542 will be “9#d7”

Till now, this was a serial generation still this can be easily reverse engineered the process when set of code is given in serial order.

So what next? 

I thought of changing the arrangement of the Character obviously, not randomly but in a specific order (like “9#d7” is changed to “d#97”).

So how we will distinguish in which order the code is given? For that, we add a character into it which we call “identifier”. Now this identifier will determine the order in which the code is arranged (Example: 9#d7 is arranged to d#97 in case, the first character is shifted to 3rd place, the 2nd one remains at the same position, the 3rd one is shifted to 1st and the last one remains at the end. For this arrangement, we take an identifier as “g” and insert this into the third place, now my code will look like this “d#g97” ) .

In this process, the identifier place must not be changed. If we place identifier to 3rd place, then throughout the code generation, it will be in the third place. This identifier will make this code unique till the end of the limit.

So how will we use this identifier effectively?

Remember we are thinking of serial order generation. On every next value calculation, we will change the order of the code and mark it with identifier. (For example: for 78542, we used identifier “g”, for next value 78543 we use identifier “q” and change the order from 1st to 2nd , 2nd to 1st , 3rd to 4th and 4th to 3rd ) .

Now we inserted one more character, so the limit will also increase. The new formula will be:

New Limit = previous limit * number of identifier used

In case the previous limit = 2085136

  • Number of identifier used = 2
  • New Limit = 2085136 * 2
Let ( [ charset1 ="qwertyuiopasdfghjklzxcvbnm$&#9487610235";
charset2 ="abcdefghijkl$&#mnopqrstuvwxyz5863190427";
charset3 ="$nt#07sbpfgh91widxqrze&yvaj683klumco245";
charset4 ="076$83swint#dxqrze&yvajklumcobpfgh91245";

code1 = Mod ( Number ; 38 ) + 1;
code2 = Mod ( Div ( Number ; 38 ) ; 38 ) + 1;
code3 = Mod ( Div ( Number ; 38 * 38 ) ; 38 ) + 1;
code4 = Mod ( Div ( Number ; 38 * 38 * 38 ) ; 38 ) + 1;
code5 = Mod ( Div ( Number ; 38 * 38 * 38 * 38 ) ; 38 ) + 1;

main ="$95stv#";
Kpid = Number

] ;

Case ( Mod ( Kpid ; 7 ) = 0 ; Middle ( charset1 ; code1 ; 1 ) & Middle ( charset2 ; code2 ; 1 ) 
& "$" & Middle ( charset3 ; code3 ; 1 ) & Middle ( charset4 ; code4 ; 1 ) 
& Middle ( charset3 ; code5 ; 1 ) ;

Mod ( Kpid ; 7 ) = 1 ; Middle ( charset1 ; code2 ; 1 ) & Middle ( charset2 ; code4 ; 1 ) 
& "9" & Middle ( charset3 ; code1 ; 1 ) & Middle ( charset4 ; code5 ; 1 ) 
& Middle ( charset3 ; code3 ; 1 ) ;

Mod ( Kpid ; 7 ) = 2 ; Middle ( charset1 ; code5 ; 1 ) & Middle ( charset2 ; code1 ; 1 ) 
& "5" & Middle ( charset3 ; code2 ; 1 ) & Middle ( charset4 ; code3 ; 1 ) 
& Middle ( charset3 ; code4 ; 1 ) ;

Mod ( Kpid ; 7 ) = 3 ; Middle ( charset1 ; code3 ; 1 ) & Middle ( charset2 ; code5 ; 1 ) 
& "s" & Middle ( charset3 ; code4 ; 1 ) & Middle ( charset4 ; code1 ; 1 ) 
& Middle ( charset3 ; code2 ; 1 ) ;

Mod ( Kpid ; 7 ) = 4 ; Middle ( charset1 ; code4 ; 1 ) & Middle ( charset2 ; code3 ; 1 ) 
& "t" & Middle ( charset3 ; code5 ; 1 ) & Middle ( charset4 ; code2 ; 1 ) 
& Middle ( charset3 ; code1 ; 1 ) ;

Mod ( Kpid ; 7 ) = 5 ; Middle ( charset1 ; code4 ; 1 ) & Middle ( charset2 ; code2 ; 1 ) 
& "v" & Middle ( charset3 ; code1 ; 1 ) & Middle ( charset4 ; code3 ; 1 ) 
& Middle ( charset3 ; code5 ; 1 ) ;

Mod ( Kpid ; 7 ) = 6 ; Middle ( charset1 ; code5 ; 1 ) & Middle ( charset2 ; code4 ; 1 ) 
& "#" & Middle ( charset3 ; code3 ; 1 ) & Middle ( charset4 ; code1 ; 1 ) 
& Middle ( charset3 ; code2 ; 1 ) ;

) )

Applications

  1. For coupon code generation
  2. Unique Id for people

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Unknown
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --