CFLib.org – Common Function Library Project

ListGetRandom(List [, Delimiter])

Last updated March 12, 2004
Download UDF

author

Brad Breaux                                       Brad Breaux

Version: 2 | Requires: ColdFusion 5 | Library: StrLib

 
Rated 1 time(s). Average Rating: 5.0

Description:
Can be used to return a random string or numeric value from a list. Just pass the list and an optional delimiter and it will return the random element.

Return Values:
Returns a random element from the list.

Example:

<CFSET numlist="1,2,3,4,5,6,7,8,9,0">
<CFSET stringlist="abc,def,ghi,jkl,mnop,qrs,tuv,wxy,z">
<CFOUTPUT>
Random number:#ListGetRandom(numlist)#<BR>
Random String:#ListGetRandom(stringlist)#<BR>
<I>Because we cache UDF templates at cflib.org, the results shown here will not be random.</I>
</cfoutput>

Parameters:

Name Description Required
List The list to grab a random element from. Yes
Delimiter The list delimiter. Defaults to a comma. No

Full UDF Source:

<cfscript>
/**
* Returns a random selection from a comma delimited list.
* Modified by Raymond Camden
*
* @param List      The list to grab a random element from. (Required)
* @param Delimiter      The list delimiter. Defaults to a comma. (Optional)
* @return Returns a random element from the list.
* @author Brad Breaux (bbreaux@blipz.com)
* @version 2, March 12, 2004
*/

function ListGetRandom(instring) {
    var delim = ",";
    var rnum = 0;
    var r = '';
    if(ArrayLen(Arguments) GTE 2) delim = Arguments[2];
    if(listlen(instring) gt 0) {
        rnum = randrange(1,listlen(instring,delim));
        r = listgetat(instring,rnum,delim);
    }
    return r;
}
</cfscript>

Search CFLib.org


Latest Additions

Shawn Porter Shawn Porter added
DeMoronize
2 hour(s) ago

Chris Carey Chris Carey added
readPropertiesFi...
1 day(s) ago

Randy Johnson Randy Johnson added
lastDayofWeek
3 day(s) ago

Frank Marion Frank Marion added
sitemapPing
7 day(s) ago

Top Rated

James Sleeman                                     QuickSort
Rated 5.0, 3 time(s)

Barney Boisvert indentXml
Rated 5.0, 3 time(s)

Nathan Dintenfass                                 queryColumnsToSt...
Rated 5.0, 3 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 3 time(s)

Created by Raymond Camden / Design by Justin Johnson