Zoho Deluge - Generate 5 Letter Booking Retrieval Code

What?
This is a quick article on how to generate a 5 letter code from several functions: one which returns 5 randomly selected characters and another two which convert a number to 5 letters.

Why?
Zoho doesn't have a function to generate random numbers or strings. There are workarounds however.

How?
The first note is that there is something that can emulate a random number... well it's not really random, it involves getting the milliseconds from a time object.
copyraw
v_Uid = zoho.currenttime.toLong();
     // returns something like 1557400230165
  1.  v_Uid = zoho.currenttime.toLong()
  2.       // returns something like 1557400230165 

Method #1: 5 Randomly Selected Letters

Using a snippet from the community forums on how someone shuffled a pack of cards, I came up with a function that will take a list of letters, shuffle the list, and return the first 5 characters.
copyraw
string generateRetrievalCode()
{
    // init
    v_Output = "";
    l_Output = List:String();
    l_Alphabet = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
    v_StrLength = 5;

    // everyday I'm shufflin
    l_OutputShuffled = List();
    v_OutputListSize = l_Alphabet.size();
    for each v_Element in l_Alphabet
    {
        v_Random = (zoho.currenttime.toLong()  %  v_OutputListSize);
        l_OutputShuffled.add(l_Alphabet.get(v_Random));
        l_Alphabet.removeElement(v_Random);
        v_OutputListSize = v_OutputListSize - 1;
    }
    l_Output = l_OutputShuffled.subList(0,v_StrLength);
    v_Output= l_Output.toString("");
    return v_Output;
}
  1.  string generateRetrievalCode() 
  2.  { 
  3.      // init 
  4.      v_Output = ""
  5.      l_Output = List:String()
  6.      l_Alphabet = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}
  7.      v_StrLength = 5
  8.   
  9.      // everyday I'm shufflin 
  10.      l_OutputShuffled = List()
  11.      v_OutputListSize = l_Alphabet.size()
  12.      for each v_Element in l_Alphabet 
  13.      { 
  14.          v_Random = (zoho.currenttime.toLong()  %  v_OutputListSize)
  15.          l_OutputShuffled.add(l_Alphabet.get(v_Random))
  16.          l_Alphabet.removeElement(v_Random)
  17.          v_OutputListSize = v_OutputListSize - 1
  18.      } 
  19.      l_Output = l_OutputShuffled.subList(0,v_StrLength)
  20.      v_Output= l_Output.toString("")
  21.      return v_Output; 
  22.  } 
This is my preferred method and it will return a string of 5 random uppercase letters.

Method #2: Convert time to letters
I've previously used substring to extract the milliseconds into 5 numbers and get the remainder from dividing it by 26 (modulus)... This is NOT to be used for Booking Retrieval Codes as it is easy to determine what code if you have the date/time of the booking as well as the customer's surname (method #1 would not be guessed by date/time):
copyraw
string generateRetrievalCode()
{
    // init
    v_Output = "";
    l_Output = List:String();
    l_Alphabet = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
    v_StrLength = 5;

    // convert milliseconds into 5 parts
     v_Uid = zoho.currenttime.toLong();
     v_UidStr = v_Uid.toString().trim();
     l_Parts.add( v_UidStr.substring(0,3) );
     l_Parts.add( v_UidStr.substring(3,6) );
     l_Parts.add( v_UidStr.substring(6,8) );
     l_Parts.add( v_UidStr.substring(8,10) );
     l_Parts.add( v_UidStr.substring(10,12) );

    // loop through parts
    for each v_Element in l_Parts
    {
        v_Remainder = v_Element.toLong() % 26;
        l_Output.add( l_Alphabet.get( v_Remainder ) );
    }

    // output
    v_Output= l_Output.toString("");
    return v_Output;
}
  1.  string generateRetrievalCode() 
  2.  { 
  3.      // init 
  4.      v_Output = ""
  5.      l_Output = List:String()
  6.      l_Alphabet = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}
  7.      v_StrLength = 5
  8.   
  9.      // convert milliseconds into 5 parts 
  10.       v_Uid = zoho.currenttime.toLong()
  11.       v_UidStr = v_Uid.toString().trim()
  12.       l_Parts.add( v_UidStr.substring(0,3) )
  13.       l_Parts.add( v_UidStr.substring(3,6) )
  14.       l_Parts.add( v_UidStr.substring(6,8) )
  15.       l_Parts.add( v_UidStr.substring(8,10) )
  16.       l_Parts.add( v_UidStr.substring(10,12) )
  17.   
  18.      // loop through parts 
  19.      for each v_Element in l_Parts 
  20.      { 
  21.          v_Remainder = v_Element.toLong() % 26
  22.          l_Output.add( l_Alphabet.get( v_Remainder ) )
  23.      } 
  24.   
  25.      // output 
  26.      v_Output= l_Output.toString("")
  27.      return v_Output; 
  28.  } 

Method #3: randomNumber() function
Using the randomNumber() function we can hope this works reliably:
copyraw
string generateRetrievalCode()
{
    // init
    v_Output = "";
    l_Output = List;
    l_Alphabet = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
    l_StrLength = {0,1,2,3,4};

    // assign a letter to a list entry
    for each index  v_Index in l_StrLength
    {
        l_Output.add( l_Alphabet.get(randomNumber(0, 25)) );
    }

    // output
    v_Output= l_Output.toString("");
    return v_Output;
}
  1.  string generateRetrievalCode() 
  2.  { 
  3.      // init 
  4.      v_Output = ""
  5.      l_Output = List; 
  6.      l_Alphabet = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}
  7.      l_StrLength = {0,1,2,3,4}
  8.   
  9.      // assign a letter to a list entry 
  10.      for each index  v_Index in l_StrLength 
  11.      { 
  12.          l_Output.add( l_Alphabet.get(randomNumber(0, 25)) )
  13.      } 
  14.   
  15.      // output 
  16.      v_Output= l_Output.toString("")
  17.      return v_Output; 
  18.  } 

Additional Notes:
As this was for a client who arranged outdoor activities for kids, I need to add a filter which blocks out swear words and the such. Experimental - DO NOT USE:
copyraw
string generateRetrievalCode()
{
       // ... refer to above functions
}
string generateKidSafeRetrievalCode()
{
    // init
    v_Output = "";
    l_BadWords = {"My","Bad","Words"};
    l_TimesTried = {1,2,3,4,5};

    // loop through times you will chance it
    for each v_TimeTried in l_TimesTried
    {
         // init
         b_BadWordFound = false;

         // get attempt
         v_Output = thisapp.generateRetrievalCode();

         // see if can use contains but check for word
         for each v_BadWord in l_BadWords
         {
              if(v_Output.indexOf( v_BadWord ) >= 0)
              {
                   b_BadWordFound = true;
              }
         }

         // if not found
         if(!b_BadWordFound)
         {
              break;
         }
    }

    // output
    return v_Output;
}
  1.  string generateRetrievalCode() 
  2.  { 
  3.         // ... refer to above functions 
  4.  } 
  5.  string generateKidSafeRetrievalCode() 
  6.  { 
  7.      // init 
  8.      v_Output = ""
  9.      l_BadWords = {"My","Bad","Words"}
  10.      l_TimesTried = {1,2,3,4,5}
  11.   
  12.      // loop through times you will chance it 
  13.      for each v_TimeTried in l_TimesTried 
  14.      { 
  15.           // init 
  16.           b_BadWordFound = false
  17.   
  18.           // get attempt 
  19.           v_Output = thisapp.generateRetrievalCode()
  20.   
  21.           // see if can use contains but check for word 
  22.           for each v_BadWord in l_BadWords 
  23.           { 
  24.                if(v_Output.indexOf( v_BadWord ) >= 0) 
  25.                { 
  26.                     b_BadWordFound = true
  27.                } 
  28.           } 
  29.   
  30.           // if not found 
  31.           if(!b_BadWordFound) 
  32.           { 
  33.                break
  34.           } 
  35.      } 
  36.   
  37.      // output 
  38.      return v_Output; 
  39.  } 

Source(s):
Category: Zoho :: Article: 678

Please publish modules in offcanvas position.