Id Generator – How To Write Code In Difference Programming Language

This small tutorial will discuss in regard to how to port a very small but functional library into difference coding platforms. This code function is an ID Generator. What this small function does is it will take in one or three input numbers and the length format of the ID block. For each number that was inputted the program will generate 1 block of random numbers from a defined set of characters.

This type of generator is good for situations where security is a concern and we do not want an account holder to be able to predict another account holder user’s ID. However, we would still want the flexibility of being able to determine the category of which the account fall under by simply looking at the ID.

So what does this code take in as input? It will take in four values, the first three values are the none random ID numbers that we would like to have. Each value is would be a block of none random number within the ID. The program will not accept any negative value for any argument. The fourth value is the length of each ID block. This will be fill in with 0 to make up the complete block where the ID number is not large enough to fill the length. The minimum length is 2. If there is not an input for length then the script default value is 5. We can skip out the second and third value block simply passing a 0 value as an input.

This is an example of the program’s output, idGenerator(1,0,0,5) would produce xxxxx-00001 -> In this example where xxxxx will be five random characters.

Another example would be, idGenerator(1,3,10,4) would produce xxxx-0001-xxxx-0003-xxxx-0010 -> In this example where x are, it will be replaced with a random character.

Most of the tutorial are written as comments inside the script.

function idGenerator(n1, n2, n3, lead0){
            //Javascript function declares with the word function, then the name and with any variable(s) that need to be pass to the function enclose inside (). All code for the function goes in between {}.
            //Ending of Javascript code line or variable declaration is a ; 
            // Javascript declares variable with the var keyword and then the variable name.

            function getRandom(min, max) {
 	        // This function is to get a random number. Javascript library does not have a native random number generator thus we use the Math library.
            // The math library random will generate a random floating point number. We would then need to multiply the value to (max number - min number). After the previous equation, we would then add the minimum number from the defined random range.
                return Math.random() * (max - min) + min;
            // This demonstrate how to use prototype.
            // I declared pad to be the prototype of the number type.
            // Which in terms now if i have any variables that are a number type, I can put a dot and then pad to call the pad function.
            // Imaging if s was the variable name. I can call function pad by typing s.pad();
            Number.prototype.pad = function(size) {
     	    // Although not passing itself into the function directly but when initialising the call, the variable with this prototype can be call as an object.
                var s = String(this);

            // Java while loop to pad 0 in front of a number until it reach a length.
      	    while (s.length < (size || 1)) {s = "0" + s;}
      		return s;

            // Check to see if anything is undefined and set a default value.            
            if(typeof(n1) === 'undefined') n1 = 1;
            if(typeof(n2) === 'undefined') n2 = 0 ;
            if(typeof(n3) === 'undefined') n3 = 0 ;
            if(typeof(lead0) === 'undefined') lead0 = 5;
            // If anything is negative return error and exit the code.
            if(n1 < 0 || n2 < 0 || n3 < 0 || lead0 < 0){
                var error = "can't work with negative"; 
                return error;
            // If n2 does not have a value and n3 have a value then we also return error and exit
            if(n2 < 1 && n3 > 0){
                var error = "input have to follow order"; 
                return error;
            // If user input something less than 2 for the fourth input then return error and exit
            if(lead0 < 2){
                 var error="need at least 2";
                 return error;
            // if n1 is equal to 0 then we set it to 1 otherwise it will be equal to input
            var n1 = n1 === 0? 1 : n1;

            // When we get a random number. We use that random number as a position to grab a character from this alpha string.
            var alpha = "abcdefghijklmnopqrstuvwxyz123456789";

            // The random ID string we going to use to join all the random character into one so we can have a string of random character
            var idstr = "";

            // All input that is not random is pad with the appropriate length.
            n1out = (n1).pad(lead0);
            n2out = (n2).pad(lead0);
            n3out = (n3).pad(lead0);
            //The random block variable tell the generator how many blocks of random numbers it should generate base on how many numbers were inputted.
            rdblk = 0;
            //The random array is used to store the string of array text
            rdarr = [];
            // We set the amount of random block we need. Each input the script have will increase the random block count by one
            if (n1 > 0){
               rdblk = rdblk +1;
            if (n2 > 0){
               rdblk = rdblk +1;
            if (n3 > 0){
               rdblk = rdblk +1

            // In Javascript, this is the "for" loop. It will first declare a variable which is "i" in this case.
            // The second is the condition of execution. This loop will execute while its condition is true.
            // The third is what the loop does after one round of execution. 
            // For every random block of numbers, we would need to run through this loop once.
            for (i = 0; i < rdblk; i++) {
                // a is a counter for our while loop.
                var a = 0;
                // b is our random ID string.
                var b = "";
                // This while loop will execute as long as a is less than lead0 which is the length that we wanted for our ID block
                // Each time the while loop go our random ID string add a random character to it. After that our counter variable a will plus one.
                 while (a < lead0){
                   b += alpha.charAt(getRandom(0, alpha.length) );
                rdarr[i] = b;

            // Since we know how many ID blocks we can have at maximum and minimum, we can manually add the random ID block to the none random ID block. 
            if (rdblk == 3){
                 idstr = rdarr[0]+ "-" +n1out+ "-" +rdarr[1]+ "-" +n2out+ "-" +rdarr[2]+ "-"+n3out
            if (
                rdblk == 2){idstr = rdarr[0]+ "-" +n1out+ "-" +rdarr[1]+ "-" +n2out
            if (rdblk == 1){
                idstr = rdarr[0]+ "-" +n1out

            // Returning the id string
            return idstr;

Most of the instruction on how the function going to operate were written into the Javascript version. Now let us move onto Groovy and see if there is anything change.

public idGenerator(int n1 = 0, int n2 = 0, int n3 = 0, int lead0 = 5){
    // Our if loop is still the same. However, in Groovy we can now set the default value for function variable if they are input into the function declaration.
   // Instead of using the var keyword to declare all variable now use int or String to declare them.
   // At the end of our line of code, we do not need the semi column to end the code anymore. Unless we wanted to combine two line of code into one.
    if (n1 < 0 || n2 < 0 || n3 < 0 || lead0 < 0){
        return "can't work with negative"
    if (n2 < 1 &&  n3 > 0){
        return "input have to follow order"
    if (lead0 < 2){
        return "need at least 2"
    n1 = (n1 == 0)? 1 : n1 
    format = "%0"+lead0+"d"
    String alpha = "abcdefghijklmnopqrstuvwxyz123456789"
    String idstr
    //Now we have a string format function, and doesn't need to produce the prototype padding for number.
    n1out = String.format(format, n1) 
    n2out = String.format(format, n2)
    n3out = String.format(format, n3)
    rdblk = 0
    rdarr = []
    if (n1 > 0){
      rdblk = rdblk +1
      if (n2 > 0){
       rdblk = rdblk +1
      if (n3 > 0){
       rdblk = rdblk +1
    // The for loop is still the same like JavaScript.
    for (i = 0; i < rdblk; i++) { 
       // Groovy have a built in Random library that we can use that does not requires additional step to get a whole random digit.
       // This Random() function would generate one to the amount of random number that were defined by argument four. Each times the Random() function execute, it would collect a character from the alpha string above.
       // After doing so it will join all the character into a string and then store the string into our random array.
       rdarr[i] = new Random().with { (1..lead0).collect { alpha[nextInt( alpha.length() ) ] }.join()}
    if(rdblk == 3){
      idstr = rdarr[0]+ "-" +n1out+ "-" +rdarr[1]+ "-" +n2out+ "-" +rdarr[2]+ "-"+n3out
    if(rdblk == 2){
      idstr = rdarr[0]+ "-" +n1out+ "-" +rdarr[1]+ "-" +n2out
    if(rdblk == 1){
      idstr = rdarr[0]+ "-" +n1out
    // The return statement is generally the same across programming platform.
    return idstr

Let continue to the PHP version and let see how much we need to change to adapt our function to the new script. Sometimes it is good to understand mathematical equations and procedures of the program we are writing. With a good and thoroughly understanding of the fundamental of how our program operates and need to achieve, sometimes we can write programs that can adapt to every language by using the simplest function to write our program. Then when we port our program to another coding platform we do not have to modify extensively on the framework.

function idGenerator($n1 = 0, $n2 = 0, $n3 = 0, $lead0 = 5){
    // Our if still look the same, in fact it is.
    // We now need to put a semi column at the end of our line of code.
    // We still can declare a default value for variable when we declare the function.
    // One thing is change for sure, we do not use the var or String or int keyword to declare our variable.
    // We simply declare variables with the $ sign in front of our variable name.
    if($n1 < 0 || $n2 < 0 || $n3 < 0 || $lead0 < 0){
        return "can't work with negative";
    if($n2 < 1 && $n3 > 0){
        return "input have to follow order";
    if($lead0 < 2){
        return "need at least 2";
    $n1 = ($n1 == 0)? 1 : $n1; 
    $format = "%'.0".$lead0."d";
    $alpha = "abcdefghijklmnopqrstuvwxyz123456789";

    // We would have the same formating function like Groovy. However, it is now turn into a complete sprintf function.
    $n1out = sprintf($format, $n1);
    $n2out = sprintf($format, $n2); 
    $n3out = sprintf($format, $n3);
    $rdblk = 0;
    // Our array declaration is now change instead of something=[]; we now have $something = array();
    $rdarr = array();
    if($n1 > 0){
       $rdblk = $rdblk +1 ;
    if($n2 > 0){
       $rdblk = $rdblk +1;
    if($n3 > 0){
       $rdblk = $rdblk +1 ;
    $rdstr = "";
    // The for loop still the same
    for ($i = 0; $i < $rdblk; $i++) { 
        for ($id = 0; $id < $lead0; $id++){
            // Php have simpler random function. All I have to input into the arguments are rand(min, max)
            // We are minusing 1 is because the string start at 0 and not one thus a 50 length string start at position 0 and end at position 49.
            $rdstr = $rdstr . $alpha[rand(0,strlen($alpha)-1)];
    $rdarr[$i] = $rdstr; $rdstr = "";
    if ($rdblk == 3){
        $idstr = $rdarr[0]. "-" .$n1out. "-" .$rdarr[1]. "-" .$n2out. "-" .$rdarr[2]. "-".$n3out;
    if ($rdblk == 2){
        $idstr = $rdarr[0]. "-" .$n1out. "-" .$rdarr[1]. "-" .$n2out;
    if ($rdblk == 1){
        $idstr = $rdarr[0]. "-" .$n1out;
    // This return statement is pretty much the same for a majority of programming language.
    return $idstr;

echo idGenerator();

This post was written by Kevin and was first post @
Original Post Name: "Id Generator – How To Write Code In Difference Programming Language".
Original Post Link:

The easiest way to create a website for your business. Create your site at!

Random Article You May Like

One thought on “Id Generator – How To Write Code In Difference Programming Language

  1. This is the perfect webpage for anyone wwho hopes to
    find out about thus topic. You understand a whole loot its almost hard
    to argue with you (not that I actually will need to…HaHa).
    You definitely put a new spin on a subject which has been written about for many years.
    Great stuff, just wonderful!

Leave a Reply

Your email address will not be published. Required fields are marked *