CFLib.org – Common Function Library Project

getRandString(stringLenth)

Last updated February 03, 2004

author

Kenneth Rainey

Version: 1 | Requires: CF5 | Library: StrLib

Description:
getRandString is an easy-to-use random string generator. Pass it a integer, and it returns a random alphanumeric string of the specified length.

Return Values:
Returns a string.

Example:

<cfoutput>#getRandString(15)#</cfoutput>

Parameters:

Name Description Required
stringLenth Length of random string to generate. Yes

Full UDF Source:

/**
 * Returns a random alphanumeric string of a user-specified length.
 * 
 * @param stringLenth      Length of random string to generate. (Required)
 * @return Returns a string. 
 * @author Kenneth Rainey (kip.rainey@incapital.com) 
 * @version 1, February 3, 2004 
 */
function getRandString(stringLength) {
    var tempAlphaList = "a|b|c|d|e|g|h|i|k|L|m|n|o|p|q|r|s|t|u|v|w|x|y|z";
    var tempNumList = "1|2|3|4|5|6|7|8|9|0";
    var tempCompositeList = tempAlphaList&"|"&tempNumList;
    var tempCharsInList = listLen(tempCompositeList,"|");
    var tempCounter = 1;
    var tempWorkingString = "";
    
    //loop from 1 to stringLength to generate string
    while (tempCounter LTE stringLength) {
        tempWorkingString = tempWorkingString&listGetAt(tempCompositeList,randRange(1,tempCharsInList),"|");
        tempCounter = tempCounter + 1;
    }
    
    return tempWorkingString;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson