Generating Random Strings
The following function generates random data using a format string that you can customize. The following example can be found in the RandomString.vbs file located in the <Installdir>\CodeSamplesPlus folder.
' VBScript source code
Randomize (timer)
’ Declaration of all formats
’ Note that a token contained within a second token (e.g. yy to yyyy) must be placed AFTER the containing token (yyyy must precede yy)
FormatArray = Array("#", "DAY", "MONTH", "dd", "mm", "yyyy", "yy", "NAME", "COLOR", "CAR")
’ Definition of formats not appearing as a Case in GetRandStrForToken
Dim ValuesArray
ValuesArray = _
Array(_
Array("DAY", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"),_
Array("MONTH", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"),_
Array("COLOR", "Red", "Yellow", "Blue", "Green"),_
Array("NAME", "Hilik", "Oren", "Shoshi", "Ilan"),_
Array("CAR", "Ford", "Toyota", "Lexus", "Mazda")_
)
’ Generate a random value for a specific token
Function GetRandStrForToken(sToken)
Select Case sToken
Case "#"' Single digit
GetRandStrForToken = CStr(Int(Rnd*10))
Case "dd"' May (numeric)
GetRandStrForToken = CStr (Int(Rnd*31 + 1))
Case "mm"' Month (numeric)
GetRandStrForToken = CStr (Int(Rnd*12 + 1))
Case "yy"' Two digit Year
GetRandStrForToken = CStr(Int(Rnd*10)) + CStr(Int(Rnd*10))
Case "yyyy"' Four digit Year
GetRandStrForToken = CStr (1950 + Int(Rnd * 100))
Case Else ' Pick from the Values Array for other formats
'LookUpArray
For i = 0 to UBound(ValuesArray)
If sToken = ValuesArray(i)(0) Then
GetRandStrForToken = ValuesArray(i)( 1 + Int( rnd*(UBound(ValuesArray(i))) ) )
Exit For
End If
Next
End Select
End Function
’ Parse the current token (if any)
Function GetNextToken (ByRef sFormat)
For each sToken in FormatArray
sTemp = Mid(sFormat, 1, Len(sToken))
If sTemp = sToken Then
GetNextToken = sToken
sFormat = Mid(sFormat, Len(sToken) + 1)
Exit Function
End If
Next
End Function
’ Generate random data given a format
Function GenerateRandData(ByVal Format)
Dim nPos
Dim nLength
nLength = Len(Format)
While nLength > 0
’ Anything inside a [] brackets is copied as is
If Mid(Format, 1, 1) = "[" Then ' Find the closing brackets
nPos = InStr(1, Format, "]", vbTextCompare)
If nPos = 0 Then Exit Function
GenerateRandData = GenerateRandData + Mid(Format, 2, nPos - 2)
Format = Mid(Format, nPos + 1)
nLength = Len(Format)
Else
’ Search for a valid token
sToken = GetNextToken(Format)
If Not sToken = "" Then
GenerateRandData = GenerateRandData + GetRandStrForToken(sToken)
nLength = Len(Format)
Else ' No token - just copy the current character
GenerateRandData = GenerateRandData + Mid(Format,1, 1)
If nLength = 1 Then
Exit Function
End If
Format = Mid(Format, 2)
nLength = Len(Format)
End If
End If
Wend
End Function
’ Usage
msgbox GenerateRandData ("Today Is the first DAY in MONTH")
msgbox GenerateRandData ("A Date in the format [dd/mm/yyyy] dd/mm/yyyy")
msgbox GenerateRandData ("My name is NAME, I drive a COLOR CAR. you can reach me on +972-54-###-####")