CFLib.org – Common Function Library Project

randomGenerator(count, start, end)

Last updated December 1, 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;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson