JerryD Posted May 12, 2006 Share Posted May 12, 2006 In the post Getting Variables Out Of An Array ?, jslegers was trying to load a one dimension array with random numbers. I was intrigued by the idea, and came up with a UDF to do just that._ArrayRandomize is a UDF that will fill a one dimension array with random numbers, and returns a string containing those numbers separated by a delimiter. The function uses the Random() and optionally the _ArraySort() function, and adheres to their default values and parameters.expandcollapse popup;============================================================================================== ; _ArrayRandomize(ByRef $aAry, $iMin = 0, $iMax = 1, $iRFlag = 0, $iSort = 1, $iDesc = 0, $sDelim = ',') ; Description: Load a one dimension array with random values ; Parameters: $aAry - one dimension array to be filled with random numbers ; $iMin - The smallest number to be generated ; 0 (DEFAULT) ; $iMax - The largest number to be generated ; 1 (DEFAULT) ; $iRFlag - If this is set to 1 then an integer result will be returned ; 1 - Integer returned (DEFAULT and set if $iMax > 1) ; 0 - floating point number returned (DEFAULT unless $iMax >1) ; $iSort - Whether or not to sort the array (and therefore the string returned) ; 0 or -1 - Do NOT sort the array (or the string returned) ; 1 - Sort the array (DEFAULT) ; $iDesc - Sort Descending or Ascending ; 0 = Sort Descending (DEFAULT) will also apply if value passed is -1 ; any other value - Sort Ascending ; $sDelim - Delimiter character (or string) to put between values in string ; DEFAULT is ',' ; Requirements: AutoIt 3.1.1.x Beta, Array.au3 (standard #include) ; Return Value: On Success - String with random numbers separated by a delimiter (default) ; On Failure - '' and Set @ERROR to: 1 ; Author: JerryD ; Note(s): A value of -1 can be used for all optional parameters for their default values. ;============================================================================================== Func _ArrayRandomize(ByRef $aAry, $iMin = 0, $iMax = 1, $iRFlag = 0, $iSort = 1, $iDesc = 0, $sDelim = ',') ; Make sure $aAry is a one dimenstion array If Not IsArray($aAry) Or UBound($aAry, 0) <> 1 Then SetError(1) Return '' EndIf ; Local Variables Local $x, $y, $Cnt = UBound($aAry) - 1, $sRet = '' ; If optional paramaters are -1, reset them to their default values If $iMin = -1 Then $iMin = 0 If $iMax = -1 Then $iMax = 1 If $iMax > 1 Then ; If $iMax > 1 then $iRFlag must be 1 $iRFlag = 1 ElseIf $iRFlag = -1 Then $iRFlag = 0 EndIf If $iSort = -1 Then $iSort = 0 If $iDesc = -1 Then $iDesc = 0 For $x = 0 To $Cnt $aAry[$x] = Random($iMin, $iMax, $iRFlag) For $y = 0 To $x If $y = $x Then ExitLoop EndIf While $aAry[$y] = $aAry[$x] $aAry[$x] = Random($iMin, $iMax, $iRFlag) WEnd Next Next If $iSort Then _ArraySort($aAry, $iDesc, 0) EndIf For $x = 0 To $Cnt $sRet &= $aAry[$x] & $sDelim Next If $sDelim <> '' Then $sRet = StringTrimRight($sRet, StringLen($sDelim)) EndIf Return $sRet EndFunc ;==>_ArrayRandomizeDownload the code from here:http://www.autoitscript.com/fileman/users/JerryD/_ArrayRandomize.au3Jerry Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now