Function: String.RandomFromPattern
Last Revision: October 28, 2004 (001)
Information
This function can be used to generate a random string based on a given pattern. This is similar in function to Setup Factory 6.0's "Generate Random Value" action.
To use this function in your Setup Factory 7.0 projects, simply copy and paste it into your Global Functions. You can get there by selecting Resources > Global Functions from the main menu.
To call the function from your project, try copying and pasting the following script onto an action tab, such as the On Startup event. You can get there by choosing Project > Actions.Code:--[[ FUNCITON: String.RandomFromPattern PURPOSE: To return a random string based on a given pattern. PARAMETERS: Pattern (string) - The pattern to base the random string on. Interpreted as follows: # - A random digit (0-9) * - A random uppercase letter (A-Z) @ - A random lowercase letter (a-z) ? - A random digit or uppercase letter (0-9,A-Z) ~ - A random digit, upppercase or lowercase letter (0-9,A-Z,a-z) Any other charater - Remains the same. RETURNS: A randomized string. EXAMPLES: String.RandomFromPattern("###-###"); -- could produce: "6355-0989" String.RandomFromPattern("TEST-***"); -- could produce: "TEST-HAJ" String.RandomFromPattern("~~~~~~~~~~"); -- could produce: "7Hg543fp02" ]] function String.RandomFromPattern(Pattern) local strReturn = ""; local tblAlphabet = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N", "O","P","Q","R","S","T","U","V","W","X","Y","Z"}; -- Seed the random number engine with the current time... local nRandomSeed = 0; nRandomSeed = String.ToNumber(System.GetTime(TIME_FMT_HOUR)); nRandomSeed = nRandomSeed * String.ToNumber(System.GetTime(TIME_FMT_MIN)); nRandomSeed = nRandomSeed * String.ToNumber(System.GetTime(TIME_FMT_SEC)); Math.RandomSeed(nRandomSeed); local nStringLength = String.Length(Pattern); for i = 1,nStringLength do local nChar = String.Mid(Pattern,i,1); if(nChar == "#")then -- Number nChar = Math.Random(0,9); elseif(nChar == "*")then -- Uppercase letter nChar = tblAlphabet[Math.Random(1,26)]; elseif(nChar == "@")then -- Lowercase letter nChar = String.Lower(tblAlphabet[Math.Random(1,26)]); elseif(nChar == "?")then -- Number or Uppercase letter local nCoinToss = Math.Random(1,2); if(nCoinToss == 1)then -- Number nChar = Math.Random(0,9); else -- Uppercase Letter nChar = tblAlphabet[Math.Random(1,26)]; end elseif(nChar == "~")then -- Number or Uppercase letter or Lowercase Letter local nCoinToss = Math.Random(1,3); if(nCoinToss == 1)then -- Number nChar = Math.Random(0,9); elseif(nCoinToss == 2)then -- Uppercase Letter nChar = tblAlphabet[Math.Random(1,26)]; else -- Lowercase letter nChar = String.Lower(tblAlphabet[Math.Random(1,26)]); end end strReturn = strReturn..nChar; end return strReturn; end
Function Name: String.RandomFromPatternCode:-- Make a random serial number local strSerial = String.RandomFromPattern("PROD-####-####-####-####"); Dialog.Message("Random Serial Number",strSerial);
Type: Setup Factory 7.0 Function
Created By: Brett Kapilik
Comments? Please post them here.

Reply With Quote