Indigo Rose Software
  #1  
Old 10-28-2004
Brett's Avatar
Brett Brett is offline
Indigo Rose Staff Member
 
Join Date: Jan 2000
Posts: 2,001
Post Function: String.RandomFromPattern

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.

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
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:
-- Make a random serial number
local strSerial = String.RandomFromPattern("PROD-####-####-####-####");
Dialog.Message("Random Serial Number",strSerial);
Function Name: String.RandomFromPattern
Type: Setup Factory 7.0 Function
Created By: Brett Kapilik

Comments? Please post them here.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function: FindAndCloseProcessByName Brett Setup Factory 8.0 Examples 11 4 Weeks Ago 09:06 AM
Function: IsRunningOnTabletPC Ted Sullivan Setup Factory 8.0 Examples 4 06-24-2006 10:46 PM
Bizarre Callback Function Behaviour - Can anyone help? SRJ AutoPlay Media Studio 5.0 3 06-30-2004 10:16 PM
Function: Increase/Decrease Audio Playback Volume Adam AutoPlay Media Studio 5.0 Examples 0 05-31-2004 03:28 PM
Function: Resize & Center an Image kpsmith AutoPlay Media Studio 5.0 Examples 0 05-17-2004 02:36 PM


All times are GMT -6. The time now is 08:16 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright © 2000 - 2009 Indigo Rose Corporation. All rights reserved.
Indigo Rose Software