cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Generate random 6 digit from a given letterset

Pakhi
New Contributor III

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 7

ForbinCSD
Contributor

@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
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.

@Pakhi take note ^