January 1, 2005

WinTask - RandStr$

'
' RandStr$(<picture>)
'

' Author: Joe Strazzere
'

' Returns a random string based on the picture supplied.
'
' This can be useful for generating large amounts of psuedo-random data for testing.
'
' <picture> is a string with special characters interpreted as follows:
'
' 9 = generate a random number (0-9) in its place
' A = generate a random alphabetic character (A-Z, a-z) in its place
' U = generate a random uppercase alphabetic character (A-Z) in its place
' L = generate a random lowercase alphabetic character (a-z) in its place
' N = generate a random alphanumeric character (A-Z, a-z, 0-9) in its place
' X = generate any character in its place
' = an escape character.  Anything following this character is placed in the result string,
'     even if it is a picture character.  Two backslashes () cause a single backslash to be
'     placed into the result string.
' any other character is simply copied into the result string
'
' RandStr("999-999-9999") might generate              012-303-1212
' RandStr("999 ULLLL Street") might generate          801 Ymksa Street
' RandStr("XXX") might generate                       *K2
' RandStr("C: empAAA999.fil") might generate        C: empkOq474.fil
'

Function RandStr$(test$)
    Local i
    Local a
    Local new$
    Local work$
    work$=""
    i=1

    While i<= len(test$)
        Select Case Mid$(test$,i,1)
            Case ""
                 i=i+1
                 new$=Mid$(test$,i,1)
            Case "9"
                 new$=str$(Random(9))
            Case "X"
                 new$=Chr$(Random(93)+33)
            Case "U"
                 new$=Chr$(Random(25)+65)
            Case "L"
                 new$=Chr$(Random(25)+97)
            Case "A"
                 Repeat
                     a = Random(57)+65
                 Until a < 91 Or a >96
                 new$=Chr$(a)
            Case "N"
                 Repeat
                     a = Random(74)+48
                 Until a < 58 Or (a >64 And a <91) Or a > 96
                 new$=Chr$(a)
            Case Else
                 new$=Mid$(test$,i,1)
        EndSelect
        work$=work$+new$
        i=i+1
    Wend
    RandStr$=work$
EndFunction

 

'test$=("99999")                       ' like US zipcodes
'test$=("999-99-9999")                 ' like US Social Security Numbers

'test$=("(999) 999-9999")               ' like US Phone Numbers

'test$=("AAA-999-XXX")
'test$=("UUU-999-XXX")
'test$=("LLL-999-XXX")
'test$=("c:ULLL99999.fil")           ' random file name

'test$="9AULNX 999 AAA UUU LLL NNN XXX"

'test$="$19999.99"

 

Msgbox(test$+" = "+RandStr$(test$)+" "+RandStr$(test$)+" "+RandStr$(test$))