CFLib.org – Common Function Library Project

randomGenerator(count, start, end)

Last updated December 01, 2006

author

Srinivas.V.K

Version: 1 | Requires: CF5 | Library: UtilityLib

Description:
This will generate the specified number of unique random numbers.

Return Values:
Returns an array.

Example:

<cfset myarray=randomGenerator(10,1,10)>
<cfdump var="#myarray#">

Parameters:

Name Description Required
count Number of unique random numbers. Yes
start Lower range of random numbers. Yes
end Upper range of random numbers. Yes

Full UDF Source:

/**
 * This will generate the specified number of unique random numbers.
 * 
 * @param count      Number of unique random numbers. (Required)
 * @param start      Lower range of random numbers. (Required)
 * @param end      Upper range of random numbers. (Required)
 * @return Returns an array. 
 * @author Srinivas.V.K (vksrinu@gmail.com) 
 * @version 1, December 1, 2006 
 */
function randomGenerator(count,start,end) {
    var myArray = arrayNew(1);
    var i = 0;
    var ran = 0;
            
    if((arguments.end-arguments.start+1) lt arguments.count) arguments.count = arguments.end-arguments.start+1;
    for(;arrayLen(myArray) lt arguments.count; i=i+1) {
        ran=randRange(arguments.start,arguments.end);
        //before appending random number to array,check the number in array
        if(not listFind(arrayToList(myArray),ran)) 
            arrayAppend(myArray,ran);
    }
    return myArray;
}

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