Forum Discussion

Pakhi's avatar
Pakhi
New Contributor III
4 years ago

Generate random 6 digit from a given letterset

I have a scenario where I have to generate random 6 digit code in every snaplogic execution from given letter set below.

0123456789ABCDEF

can any one help in this.

7 Replies

  • viktor_n's avatar
    viktor_n
    Contributor II

    Hi @Pakhi,

    Try this.
    sl.range(6).map(x => "0123456789ABCDEF"[Math.round(Math.random()*15)]).join('')

    Regards,
    Viktor

    • Pakhi's avatar
      Pakhi
      New Contributor III

      Thanks its working!
      Can you please explain this expression , actually how its working internally.

      • viktor_n's avatar
        viktor_n
        Contributor II

        sl.range(6) - Generates me a list with number of elements.
        Inside the parameter is specified how many elements to have the list.
        Ex. [0,1,2,3,4,5]

        After that with the map() function I iterate through that list.
        And while iterating, for each element is generated a random number between 0 and 15.

        Random number is generated with Math.random() and Math.round() functions.

        • Math.random() - Generates a decimal number between 0 and 1.
        • Math.random()*15 - Generated number is multiplied by the number of characters in the string.
        • Math.round(Math.random()*15) - Decimal number is rounded to the nearest whole number.
          Ex. 3.656 - will be rounded to 4.

        Generated random number I use it as an index from which I get character from the string.
        Ex. "0123456789ABCDEF"[4] = "3" (returns the 4th index from the string).

        Now if we look at the preview of this expression, result will be list of strings.
        Ex. ["3", "8", "A", "1", "C", "D"]

        At the end the list is joined with join() function. This function is used for transforming list into string. Parameter inside join function ('') means that it will join the elements together without nothing between the elements.

        Result at the end will be this:
        "38A1CD"

        Hope this will help understand the expression 🙂

  • @Pakhi :

    You mention that you need a “random” 6 (hexadecimal) digit code.
    But there are many types of randomness, and you don’t mention what this is for.

    I’m not trying to pry, but out of concern for safety, I have to mention three things:

    • Viktor’s solution is a very clean and straightforward one if you are looking for a “nearly unique” identifier.

    • His solution is pretty darn good but not perfect if you absolutely must have a unique identifier. Extending the solution to this cannot be done in a simple mapping expression and is better suited to a script (or an integrated executable).

    • If you are planning on using this for any form of security, it is entirely inadequate and unsafe. For that, you’ll need a Cryptographically Secure Pseudorandom Number Generator (CSPRNG). Examples of these include:

      • Yarrow (very good) or Fortuna (even better)
      • ChaCha20 - if you’re always on Linux or BSD
      • CryptGenRandom - if you’re always on Windows

    Hope this helps!

  • cdmills's avatar
    cdmills
    New Contributor II

    Strictly speaking, Math.round(Math.random()*15) disfavors 0 and F; they are only half as likely to appear as any of 1 through E are.
    Math.floor(Math.random()*16) distributes better.