Search code examples
randomlotus-noteslotusscript

Lotusscript generate random 8 characters string?


How do I generate a string of 8 characters using lotusscript in this pattern? Eg: 0E 1F A3 ZK (there are spaces after every 2 characters including the last). Each character can only be either 0-9 or A-Z (uppercased only). I've used the Randomize and Rnd method before and thinking of applying it here but I'm not sure if that's the correct way and how to achieve that. Another thing is this string is going to be saved into my document and I have a view that list each of the generated string. Which means each time a string is generated, it has to be unique. If the string generated is already used in another document, then continue generating until one that hasn't been used is generated.


Solution

  • Yes, you can use Rnd in this case too.

    Define a function getRandom() which gives you a random string in format "XX XX XX XX" with every call.

    Function getRandom() As String
        Const charList = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        getRandom = _
        Mid(charList, 35*Rnd+1, 1) + Mid(charList, 35*Rnd+1, 1) + " " + _
        Mid(charList, 35*Rnd+1, 1) + Mid(charList, 35*Rnd+1, 1) + " " + _
        Mid(charList, 35*Rnd+1, 1) + Mid(charList, 35*Rnd+1, 1) + " " + _
        Mid(charList, 35*Rnd+1, 1) + Mid(charList, 35*Rnd+1, 1)
    End Function
    
    • charList is a string with all allowed characters 0-9 and A-Z.

    • you get an random number between 1 and 36 with 35*Rnd+1. This is your index in charList to get randomly one of the characters.

    Call the function getRandom() in a cycle as long as you get a string which is not yet in your view.