; Functions ; ---------------------- ; FAS_Random2DArrayAu3 Creates/returns a 2D array of random data. Pure AutoIt code. ; FAS_Random2DArrayOpt Creates/returns a 2D array of random data. Single-threaded VB code. ; FAS_Random2DArrayOptP Creates a 2D array of data. Produces progress data in worker thread. ; Runs the same code as FAS_Random2DArrayOpt and produces progress data. ; FAS_Random2DArrayOptEx Creates/returns a 2D array of random data. Multi-threaded VB code. ; Because the VB code runs multi-threaded it can produce progress data. ; FAS_Random2DArrayLimits Number of rows and elements to decide which of the 4 functions to use. ; FAS_Random2DArrayWeight Returns minimum number of weighted array elements for progress data. ; Also returns number of string columns (takes longer than other types). ; FAS_Random2DArrayCount Returns number of rows to display progress under creation of array. ; FAS_Random2DArrayGet Returns the 2D array of random data from VB code to AutoIt code. ; FAS_Random2DArrayDel Deletes a 2D array of random data stored internally in VB code. #include-once ; Creates/returns a 2D array of random data Func FAS_Random2DArrayAu3( _ $iRows, _ ; Number of rows in the array $aColumns, _ ; Number of columns and data types in the array, see 1) in docu $sSource = "" ) ; Character source for string columns, random strings are created from these characters Local $iError = FAS_CheckColumnsArray( $aColumns ) If $iError Then Return SetError($iError,0,0) Local $iColumns = UBound( $aColumns ) Local $aColTypes[6][$iColumns+1] For $i = 0 To $iColumns - 1 If FAS_CalcColTypes( $i, $aColumns, $aColTypes, $iRows, $sSource ) Then ContinueLoop ; Convert duplicates as a percentage to number of unique rows $aColumns[$i][3] = $iRows - Int( $iRows * $aColumns[$i][3] / 100 ) If $aColumns[$i][3] Then $aColumns[$i][3] -= 1 Next ; Execute FAS_Rand2DArray function and return 2D array of random data Return FAS_Rand2DArray( $iRows, $aColumns, $aColTypes, $sSource ) EndFunc ; #################################################################################################### ; ### ### ; ### Internal functions ### ; ### ### ; #################################################################################################### Func FAS_CheckColumnsArray( ByRef $aColumns ) If IsString( $aColumns ) Then $aColumns = StringSplit( $aColumns, "", 2 ) ; 2 = $STR_NOCOUNT If Not IsArray( $aColumns ) Then ConsoleWrite( "$aColumns is not an array variable" & @CRLF ) Return 3 EndIf Local $n, $aTmp[1][4] If UBound( $aColumns, 0 ) = 1 Then $n = UBound( $aColumns ) ReDim $aTmp[$n][4] For $i = 0 To $n - 1 $aTmp[$i][0] = $aColumns[$i] Next $aColumns = $aTmp ElseIf UBound( $aColumns, 0 ) = 2 Then If UBound( $aColumns, 2 ) <> 4 Then ReDim $aColumns[UBound($aColumns)][4] Else ConsoleWrite( "$aColumns is larger than a 2D array" & @CRLF ) Return 4 EndIf EndFunc Func FAS_CalcColTypes( $i, ByRef $aColumns, ByRef $aColTypes, $iRows, $sSource ) Switch $aColumns[$i][0] Case "Strings", "String", "str", "s" If Not $sSource Then Return 1 $aColTypes[0][0] += 1 ; Row 0 $aColTypes[0][$aColTypes[0][0]] = $i If $aColumns[$i][1] < 0 Then $aColumns[$i][1] = 0 FAS_CheckMinMaxDupl( $i, $aColumns, 16, 32, 1024 ) Case "Integers", "Integer", "int", "i" $aColTypes[1][0] += 1 ; Row 1 $aColTypes[1][$aColTypes[1][0]] = $i FAS_CheckMinMaxDupl( $i, $aColumns, 0, $iRows - 1, 2147483646 ) Case "Floats", "Float", "flo", "flt", "f" $aColTypes[2][0] += 1 ; Row 2 $aColTypes[2][$aColTypes[2][0]] = $i FAS_CheckMinMaxDupl( $i, $aColumns, 0, $iRows - 1, 2147483646 ) Case "Dates", "Date", "dat", "dte", "d" $aColTypes[3][0] += 1 ; Row 3 $aColTypes[3][$aColTypes[3][0]] = $i If $aColumns[$i][1] < 0 Then $aColumns[$i][1] = 0 FAS_CheckMinMaxDupl( $i, $aColumns, 2010, 2020, 9999 ) Case "Times", "Time", "tim", "tme", "t" $aColTypes[4][0] += 1 ; Row 4 $aColTypes[4][$aColTypes[4][0]] = $i If $aColumns[$i][1] < 0 Then $aColumns[$i][1] = 0 FAS_CheckMinMaxDupl( $i, $aColumns, 0, 23, 23 ) Case "Rows/Cols", "RowsCols", "Row/Col", "RowCol", "r/c", "rc", "r", "c" $aColTypes[5][0] += 1 ; Row 5 $aColTypes[5][$aColTypes[5][0]] = $i Return 1 Case Else ConsoleWrite( """" & $aColumns[$i][0] & """ is an illegal column type" & @CRLF ) ; An illegal column type results in an empty array column EndSwitch EndFunc Func FAS_CheckMinMaxDupl( $i, ByRef $aColumns, $iMinDef, $iMaxDef, $iMax ) ; Duplicates as a percentage If Not IsInt( $aColumns[$i][3] ) Then $aColumns[$i][3] = 0 If $aColumns[$i][3] > 100 Then $aColumns[$i][3] = 100 ; Maximum value If Not IsInt( $aColumns[$i][2] ) Then $aColumns[$i][2] = $iMaxDef If $aColumns[$i][2] > $iMax Then $aColumns[$i][2] = $iMax ; Minimum value If Not IsInt( $aColumns[$i][1] ) Then $aColumns[$i][1] = $iMinDef If $aColumns[$i][1] < -$iMax Then $aColumns[$i][1] = -$iMax If $aColumns[$i][1] > $aColumns[$i][2] Then $aColumns[$i][1] = $aColumns[$i][2] EndFunc Func FAS_Rand2DArray( $iRows, ByRef $aColumns, ByRef $aColTypes, $sSource ) Local $aArray[$iRows][UBound($aColumns)] Local $iSourceLen = StringLen( $sSource ) - 1 Local $aSource = StringSplit( $sSource, "", 2 ) Local $sStr = "" Local $k, $y, $m, $dMax For $i = 0 To $iRows - 1 ; String columns For $j = 1 To $aColTypes[0][0] $k = $aColTypes[0][$j] If $i <= $aColumns[$k][3] Then For $l = 1 To Random( $aColumns[$k][1], $aColumns[$k][2], 1 ) ; 1 => Integer $sStr &= $aSource[Random( 0, $iSourceLen, 1 )] Next $aArray[$i][$k] = $sStr $sStr = "" Else $aArray[$i][$k] = $aArray[Random(0,$aColumns[$k][3],1)][$k] EndIf Next ; Integer columns For $j = 1 To $aColTypes[1][0] $k = $aColTypes[1][$j] If $i <= $aColumns[$k][3] Then $aArray[$i][$k] = Random( $aColumns[$k][1], $aColumns[$k][2], 1 ) Else $aArray[$i][$k] = $aArray[Random(0,$aColumns[$k][3],1)][$k] EndIf Next ; Float columns For $j = 1 To $aColTypes[2][0] $k = $aColTypes[2][$j] If $i <= $aColumns[$k][3] Then $aArray[$i][$k] = $aColumns[$k][1] + ( $aColumns[$k][2] - $aColumns[$k][1] ) * Random() Else $aArray[$i][$k] = $aArray[Random(0,$aColumns[$k][3],1)][$k] EndIf Next ; Date columns For $j = 1 To $aColTypes[3][0] $k = $aColTypes[3][$j] If $i <= $aColumns[$k][3] Then $y = Random( $aColumns[$k][1], $aColumns[$k][2], 1 ) $m = Random( 1, 12, 1 ) Switch $m Case 1, 3, 5, 7, 8, 10, 12 $dMax = 31 Case 4, 6, 9, 11 $dMax = 30 Case Else ; 2 $dMax = Mod( $y, 4 ) <> 0 ? 28 : Mod( $y, 400 ) = 0 ? 29 : Mod( $y, 100 ) = 0 ? 28 : 29 EndSwitch $aArray[$i][$k] = $y * 10000 + $m * 100 + Random( 1, $dMax, 1 ) Else $aArray[$i][$k] = $aArray[Random(0,$aColumns[$k][3],1)][$k] EndIf Next ; Time columns For $j = 1 To $aColTypes[4][0] $k = $aColTypes[4][$j] If $i <= $aColumns[$k][3] Then $aArray[$i][$k] = Random( $aColumns[$k][1], $aColumns[$k][2], 1 ) * 10000 + Random( 0, 59, 1 ) * 100 + Random( 0, 59, 1 ) Else $aArray[$i][$k] = $aArray[Random(0,$aColumns[$k][3],1)][$k] EndIf Next ; Row/col columns For $j = 1 To $aColTypes[5][0] $k = $aColTypes[5][$j] $aArray[$i][$k] = $i & "/" & $k Next Next Return $aArray EndFunc