Sign in to follow this  
Followers 0
address

Send file names in folder in random order

31 posts in this topic

Opt('MustDeclareVars', 1)
;...
Global $sDir = 'd:\Miscel\Pics\Old comp\';...
;...
Global $sDir = @ScriptDir & '\0', $aFiles, $sString = '|', $iRand
$aFiles = _FileSearch($sDir, '*', 1)
If @error Then Exit
ConsoleWrite('Files: ' & $aFiles[0] & @LF)
For $i = 1 To $aFiles[0]
    $iRand = Random(1, $aFiles[0], 1)
    While 1
        $iRand = Random(1, $aFiles[0], 1)
        If Not StringInStr($sString, '|' & $iRand & '|') Then
            $sString &= $iRand & '|'
            ExitLoop
        EndIf
        Sleep(2)
    WEnd
    ;Send($aFiles[$iRand])
    ConsoleWrite($i & @TAB & $iRand & @TAB & $aFiles[$iRand] & @LF)
    Sleep(10000)
Next
MsgBox(64, 'Info', 'Кончились файлы :)')
Func _FileSearch($sPath, $sFileMask = "*", $iFlag = 0, $iSubDir = 1, $iSort = 0)
    ;CreatoR; http://autoit-script.ru/index.php/topic,8266.msg55775
    Local $sOutBin, $sOut, $aOut, $sRead, $hDir, $sAttrib, $sFiles, $aMasks
    If Not StringInStr(FileGetAttrib($sPath), "D") Then
        Return SetError(1, 0, 0)
    EndIf
    If $iSubDir = 1 Then
        $sAttrib &= ' /S'
    EndIf
    If $iSort = 1 Then
        $sAttrib &= ' /O:N'
    EndIf
    Switch $iFlag
        Case 1
            $sAttrib &= ' /A-D'
        Case 2
            $sAttrib &= ' /AD'
        Case Else
            $sAttrib &= ' /A'
    EndSwitch
    $sOut = StringToBinary('0' & @CRLF, 2)
    $sPath = StringRegExpReplace($sPath, '\\+$', '')
    $sFileMask = StringRegExpReplace($sFileMask, '^;+|;+$', '')
    $sFileMask = StringRegExpReplace($sFileMask, ';{2,}', ';')
    $aMasks = StringSplit($sFileMask, ';')
    For $i = 1 To $aMasks[0]
        If StringStripWS($aMasks[$i], 8) = "" Then
            ContinueLoop
        EndIf
        $sFiles &= '"' & $sPath & '\' & $aMasks[$i] & '"'
        If $i < $aMasks[0] Then
            $sFiles &= ';'
        EndIf
    Next
    $hDir = Run(@ComSpec & ' /U /C DIR ' & $sFiles & ' /B' & $sAttrib, @SystemDir, @SW_HIDE, 6)
    While 1
        $sRead = StdoutRead($hDir, False, True)
        If @error Then
            ExitLoop
        EndIf
        If $sRead <> "" Then
            $sOut &= $sRead
        EndIf
        Sleep(10)
    WEnd
    $aOut = StringRegExp(BinaryToString($sOut, 2), '[^\r\n]+', 3)
    If @error Or UBound($aOut) < 2 Then
        Return SetError(2, 0, 0)
    EndIf
    $aOut[0] = UBound($aOut) - 1
    Return $aOut
EndFunc   ;==>_FileSearch

Why this gives me this error please?

>"C:\Program Files\1\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "d:\Miscel\Pics\МОИ СКРИПТЫ\test.au3"   
>Exit code: 0    Time: 0.176

Share this post


Link to post
Share on other sites



That's not an error message, that's just telling you that it's running your script and the parameters used when it's run.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

That's not an error message, that's just telling you that it's running your script and the parameters used when it's run.

Thank you. Can you help me that this program works? I need send file names with path that are in folder but in random order. File names shouldn't repeat and after all names are sent it should stop.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Your script has a mistake in it, one of these variables should be changed.

;... 
Global $sDir = 'd:MiscelPicsOld comp';... 
;... 
Global $sDir = @ScriptDir & '0', $aFiles, $sString = '|', $iRand

EDIT: You might also want to look at this code change below, it will GREATLY speed up the process when you're dealing with a lot of files. With this change it took between 5 and 6 seconds to process 2900 file names. With your method, I stopped it at 115 seconds because the script had basically slowed to a crawl because it's looking for file names that haven't been already sent.

;~ Global $aTemp = $aFiles ;<<<<<<<<<<<< Use this if you want the $aFiles array to have the filenames in it after this is done.

Global $Timer = TimerInit()
For $i = $aFiles[0] To 2 Step -1
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $i = ' & $i & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console <<<<<<< just using this to show you how slow the script can become
    $iRand = Random(1, $aFiles[0], 1)
;~     While 1
;~         $iRand = Random(1, $aFiles[0], 1)
;~         If Not StringInStr($sString, '|' & $iRand & '|') Then
;~             $sString &= $iRand & '|'
;~             ExitLoop
;~         EndIf
;~         Sleep(2)
;~     WEnd
    ;Send($aFiles[$iRand])
    ConsoleWrite($i & @TAB & $iRand & @TAB & $aFiles[$iRand] & @LF)
    _ArrayDelete($aFiles, $iRand) ; deletes from the array the filename just sent so it's never seen again.
    $aFiles[0] -= 1 ; updates the file count number so that you don't get subscript errors.
;~     Sleep(100)
Next
ConsoleWrite($i & @TAB & 1 & @TAB & $aFiles[1] & @LF) ; sends the last remaining file name in the array, because Random doesn't work if Min and Max are both the same number, in this case 1
_ArrayDelete($aFiles, 1)
MsgBox(64, 'Info', TimerDiff($Timer) / 1000) ; shows you how many seconds it took to complete the task.
;~ $aFiles = $aTemp ; Copies back the original contents of $aFiles if you wanted to keep the filenames in it.
Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

BrewManNH

Thank you very much but I couldn't run it.

these Global$sDir also don't work alone.

And that code below, I don't know where to put my folder to watch if it will send file names.

Edited by address

Share this post


Link to post
Share on other sites

The first mistake is that you redeclare the global variable $sDir before it has ever been used. Perhaps you will need two variables: one for the source and one for the destination folder, but I'm not sure what you mean by send file names. Where are you sending the names and what's all that regexp for?

Share this post


Link to post
Share on other sites

The first mistake is that you redeclare the global variable $sDir before it has ever been used. Perhaps you will need two variables: one for the source and one for the destination folder, but I'm not sure what you mean by send file names. Where are you sending the names and what's all that regexp for?

That's not my code so I don't know about regexp. By sending I mean command Send.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

No worries. It's good to see you trying things out. That's a good way to learn new methods, providing the code doesn't crash anything. Here's an example of a method for shuffling an array. To test this code you need to set the path to the directory you wish to get the names from.

#include <Array.au3>

_RunTest()

Func _RunTest()
    Local $sSource = @ScriptDir & "/Test_Dir" ; You need to replace with your own path.

    Local $search = FileFindFirstFile($sSource & "/*.*")

    ; Check if the search was successful
    If $search = -1 Then
        MsgBox(0, "Error", "No files/directories matched the search pattern")
        Exit
    EndIf

    Local $sDelimStr = "", $file
    While 1
        $file = FileFindNextFile($search)
        If @error Then ExitLoop
        $sDelimStr &= $file & "*" ; Choose a delimiter which can't possibly appear in the file name.
    WEnd
    FileClose($search)
    ; Close the search handle

    Local $aArray = StringSplit(StringTrimRight($sDelimStr, 1), "*", 2)
    _ArrayDisplay($aArray, "Before Shuffling")

    _ArrayRandomShuffle($aArray)
    _ArrayDisplay($aArray, "After Shuffling")
EndFunc

Func _ArrayRandomShuffle(ByRef $aArray)
    Local $iRandom
    For $i = 0 To UBound($aArray) -1
        $iRandom = Random(0, UBound($aArray) -1, 1)
        If $i <> $iRandom Then _ArraySwap($aArray[$i], $aArray[$iRandom])
    Next
EndFunc

To send the names to a text file, you need to loop through the array after it has been shuffled, using a For loop as I have done in the second function. Then using Send($aArray[$i]) inside the loop should do the trick. If you want to move the files then you can't do that using Send. Also shuffling them would make no sense.

Edited by czardas

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

czardas

Thank you, how do I put my own path correctly?

Local $sSource = @ScriptDir & "/d:MiscelPicsOld comp"

Local $sSource = @ScriptDir & "d:MiscelPicsOld comp"

don't work

Edited by address

Share this post


Link to post
Share on other sites

czardas

It works, thank you very much.

How can I Send after shuffling result (file name) with path of the file one by one with 10 sec interval after each, please?

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

BrewManNH

Thank you very much but I couldn't run it.

Of course you can't run it, it's only a portion of your code that I modified to demonstrate a more efficient way of getting random elements of the array with no repeats. You only need to replace the code in your script with this part along with your function and it will work.

these Global $sDir also don't work alone.

And that code below, I don't know where to put my folder to watch if it will send file names.

And they don't work being redeclared either, you need 2 different variables if you want to use it that way. Also, the method I gave you to randomize the list is very fast and efficient for even large arrays. Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Since I don't know where you are sending the strings it's a bit like trying to find the light switch in a dark room. You have exactly 10 seconds to set focus to the window where you want to send the strings. Try it first with notepad. I generally don't like writing code that sends random strings, since it can be unpredictable. If you tell me where you are sending these file paths, I could perhaps create something more suitable:

#include <Array.au3>

_RunTest()

Func _RunTest()
    Local $sSource = "d:/Miscel/Pics/Old comp"

    Local $search = FileFindFirstFile($sSource & "/*.*")

    ; Check if the search was successful
    If $search = -1 Then
        MsgBox(0, "Error", "No files/directories matched the search pattern")
        Exit
    EndIf

    Local $sDelimStr = "", $file
    While 1
        $file = FileFindNextFile($search)
        If @error Then ExitLoop
        $sDelimStr &= $file & "*" ; Choose a delimiter which can't possibly appear in the file name.
    WEnd
    FileClose($search)
    ; Close the search handle

    Local $aArray = StringSplit(StringTrimRight($sDelimStr, 1), "*", 2)
    ;_ArrayDisplay($aArray, "Before Shuffling")

    _ArrayRandomShuffle($aArray)
    ;_ArrayDisplay($aArray, "After Shuffling")
    _WaitSend($sSource, $aArray) ; <== Added
EndFunc

Func _ArrayRandomShuffle(ByRef $aArray)
    Local $iRandom, $iBound = UBound($aArray) -1
    For $i = 0 To $iBound
        $iRandom = Random(0, $iBound, 1)
        If $i <> $iRandom Then _ArraySwap($aArray[$i], $aArray[$iRandom])
    Next
EndFunc

Func _WaitSend($sSource, $aArray, $iSleep = 10000)
    For $i = 0 To UBound($aArray) -1
        Sleep($iSleep) ; Can also go after the next line.
        Send($sSource & "/" & $aArray[$i] & @CRLF)
    Next
EndFunc
Edited by czardas

Share this post


Link to post
Share on other sites

Of course you can't run it, it's only a portion of your code that I modified to demonstrate a more efficient way of getting random elements of the array with no repeats. You only need to replace the code in your script with this part along with your function and it will work.

And they don't work being redeclared either, you need 2 different variables if you want to use it that way. Also, the method I gave you to randomize the list is very fast and efficient for even large arrays.

Forgive me my incompetence but which variables do you hint? I would like to use it in improved way. But I don't understand which variable is for my folder.

So I put improved part, function _FileSearch (which can search within subdirectories). Now I need to understand how to put 2 different variables at the beginning. If you can hint would be much obliged.

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

Since I don't know where you are sending the strings it's a bit like trying to find the light switch in a dark room. You have exactly 10 seconds to set focus to the window where you want to send the strings. Try it first with notepad. I generally don't like writing code that sends random strings, since it can be unpredictable. If you tell me where you are sending these file paths, I could perhaps create something more suitable:

#include <Array.au3>

_RunTest()

Func _RunTest()
    Local $sSource = "d:/Miscel/Pics/Old comp"

    Local $search = FileFindFirstFile($sSource & "/*.*")

    ; Check if the search was successful
    If $search = -1 Then
        MsgBox(0, "Error", "No files/directories matched the search pattern")
        Exit
    EndIf

    Local $sDelimStr = "", $file
    While 1
        $file = FileFindNextFile($search)
        If @error Then ExitLoop
        $sDelimStr &= $file & "*" ; Choose a delimiter which can't possibly appear in the file name.
    WEnd
    FileClose($search)
    ; Close the search handle

    Local $aArray = StringSplit(StringTrimRight($sDelimStr, 1), "*", 2)
    ;_ArrayDisplay($aArray, "Before Shuffling")

    _ArrayRandomShuffle($aArray)
    ;_ArrayDisplay($aArray, "After Shuffling")
    _WaitSend($sSource, $aArray) ; <== Added
EndFunc

Func _ArrayRandomShuffle(ByRef $aArray)
    Local $iRandom, $iBound = UBound($aArray) -1
    For $i = 0 To $iBound
        $iRandom = Random(0, $iBound, 1)
        If $i <> $iRandom Then _ArraySwap($aArray[$i], $aArray[$iRandom])
    Next
EndFunc

Func _WaitSend($sSource, $aArray, $iSleep = 10000)
    For $i = 0 To UBound($aArray) -1
        Sleep($iSleep) ; Can also go after the next line.
        Send($sSource & "/" & $aArray[$i] & @CRLF)
    Next
EndFunc

Well I tried to send inside this script

For $i = 0 To 99
    $iFlag = Random(0,1,0)
    ConsoleWrite("line 1" & @LF) ; Line 1
    ConsoleWrite("line 2" & @LF) ; Line 2
    If $iFlag > 0.7 Then
        ConsoleWrite("line 3" & @LF) ; Line 3
        ConsoleWrite("line 4" & @LF) ; Line 4
    EndIf
    ConsoleWrite("line 5" & @LF) ; Line 5
Next

Where I put your code instead of Line 3 and Line 4. But It didn't go as I expected.

Thank you very much anyways, the code works perfect. It just doesn't work for me, inside For... next of other code, I suppose.

Edited by address

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

Perhaps you mean this, but I'm not certain:

Firstly replace the second line

_RunTest()

with this

For $i = 0 To 99
    $iFlag = Random(0,1,0)
    ConsoleWrite("line 1" & @LF) ; Line 1
    ConsoleWrite("line 2" & @LF) ; Line 2
    If $iFlag > 0.7 Then
        _RunTest() ; Call the function here
    EndIf
    ConsoleWrite("line 5" & @LF) ; Line 5
Next

Secondly you might also want to replace this line:

Send($sSource & "/" & $aArray[$i] & @CRLF)

with this

ConsoleWrite($sSource & "/" & $aArray[$i] & @CRLF)

Using ConsoleWrite is a good way to test things. Using Send can be unpredictable when you are testing code. You definately should spend some time on tutorials and take a look at the wiki articles.

Edited by czardas

Share this post


Link to post
Share on other sites

czardas

Thank you very much. I have slight problem with this code. First of all, my general idea is to make For... next script that makes mouseclicks than decides to send or not send one random file name from folder then mouse clicks and then starts from beginning.

For $i = 0 To 99
    $iFlag = Random(0,1,0)
    ConsoleWrite("line 1" & @LF) ; Line 1
    ConsoleWrite("line 2" & @LF) ; Line 2
    If $iFlag > 0.7 Then
        _RunTest() ; Call the function here
    EndIf
    ConsoleWrite("line 5" & @LF) ; Line 5
Next

The problem is when script goes to _RunTest part it prints all filenames, not - one and then to Fifth line (which should be mouseclics), then Line 1, Line 2, second filename (if Random allows to Send second filename this time). Can you help please?

Using ConsoleWrite is a good way to test things. Using Send can be unpredictable when you are testing code. You definately should spend some time on tutorials and take a look at the wiki articles.

Downloaded this

Share this post


Link to post
Share on other sites

@address This purpose of this script is unclear to me. If I can't understand its purpose, then I'm unable to advise you properly.

Also, the method I gave you to randomize the list is very fast and efficient for even large arrays.

I remember reading a thread about this some time back, but I couldn't find it, so I wrote the method above copying someone else's idea. The problem with using _ArrayDelete is that it is very slow by comparison. To test the difference I took your code and removed everything except _ArrayDelete. Here are the tests I ran:

#include <Array.au3>

Global $aTemplate[5000], $aTemp, $iTimer
For $i = 0 To 4999
    $aTemplate[$i] = 4999
Next

ConsoleWrite("Starting tests" & @CRLF)

; Test 1
$aTemp = $aTemplate
$iTimer = TimerInit()
_ArrayRandomDelete($aTemp)
ConsoleWrite("Test 1 >>> " & TimerDiff($iTimer) & @CRLF)

; Test 2
$aTemp = $aTemplate
$iTimer = TimerInit()
_ArrayRandomShuffle1($aTemp)
ConsoleWrite("Test 2 >>> " & TimerDiff($iTimer) & @CRLF)

; Test 3
$aTemp = $aTemplate
$iTimer = TimerInit()
_ArrayRandomShuffle2($aTemp)
ConsoleWrite("Test 3 >>> " & TimerDiff($iTimer) & @CRLF)

Func _ArrayRandomDelete($aArray)
    For $i = $aArray[0] To 2 Step -1
        $iRand = Random(1, $aArray[0], 1)
        _ArrayDelete($aArray, $iRand)
        $aArray[0] -= 1
    Next
    _ArrayDelete($aArray, 1)
EndFunc

Func _ArrayRandomShuffle1(ByRef $aArray)
    Local $iRandom, $iBound = UBound($aArray) -1
    For $i = 0 To $iBound
        $iRandom = Random(0, $iBound, 1)
        If $i <> $iRandom Then _ArraySwap($aArray[$i], $aArray[$iRandom])
    Next
EndFunc

Func _ArrayRandomShuffle2(ByRef $aArray)
    Local $iRandom, $swap, $iBound = UBound($aArray) -1
    For $i = 0 To $iBound
        $iRandom = Random(0, $iBound, 1)
        If $i <> $iRandom Then
            $swap = $aArray[$i]
            $aArray[$i] = $aArray[$iRandom]
            $aArray[$iRandom] = $swap
        EndIf
    Next
EndFunc

Share this post


Link to post
Share on other sites

How about a more accurate test, seeing as how your test pattern isn't really a valid test of how it works. Every value in the array is the same, so it's going to be faster.

#include <Array.au3>

Global $aTemplate[5000], $aTemp, $iTimer
For $i = 0 To 4999
    $aTemplate[$i] = $i ;<<<<<<<<<<<<<<< Every number in the array is different, much as how every filename is unique.
Next

ConsoleWrite("Starting tests" & @CRLF)

; Test 1
$aTemp = $aTemplate
$iTimer = TimerInit()
_ArrayRandomDelete($aTemp)
ConsoleWrite("Test 1 >>> " & TimerDiff($iTimer) & @CRLF)

; Test 2
$aTemp = $aTemplate
$iTimer = TimerInit()
_ArrayRandomShuffle1($aTemp)
ConsoleWrite("Test 2 >>> " & TimerDiff($iTimer) & @CRLF)

; Test 3
$aTemp = $aTemplate
$iTimer = TimerInit()
_ArrayRandomShuffle2($aTemp)
ConsoleWrite("Test 3 >>> " & TimerDiff($iTimer) & @CRLF)

Func _ArrayRandomDelete($aArray)
    For $i = $aArray[0] To 2 Step -1
        $iRand = Random(1, $aArray[0], 1)
        _ArrayDelete($aArray, $iRand)
        $aArray[0] -= 1
    Next
    _ArrayDelete($aArray, 1)
EndFunc   ;==>_ArrayRandomDelete

Func _ArrayRandomShuffle1(ByRef $aArray)
    Local $iRandom, $iBound = UBound($aArray) - 1
    For $i = 0 To $iBound
        $iRandom = Random(0, $iBound, 1)
        If $i <> $iRandom Then _ArraySwap($aArray[$i], $aArray[$iRandom])
    Next
EndFunc   ;==>_ArrayRandomShuffle1

Func _ArrayRandomShuffle2(ByRef $aArray)
    Local $iRandom, $swap, $iBound = UBound($aArray) - 1
    For $i = 0 To $iBound
        $iRandom = Random(0, $iBound, 1)
        If $i <> $iRandom Then
            $swap = $aArray[$i]
            $aArray[$i] = $aArray[$iRandom]
            $aArray[$iRandom] = $swap
        EndIf
    Next
EndFunc   ;==>_ArrayRandomShuffle2

You'll find that my code is by far faster than either of your 2, nearly 3x faster than your second example. :)


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

How about an even fairer test. You are only deleting one element.

#include <Array.au3>

Global $aTemplate[5000], $aTemp, $iTimer
$aTemplate[0] = 4999 ;<<<<<<<<<<<<<<< All elements need to be deleted.
For $i = 1 To 4999
    $aTemplate[$i] = $i
Next

ConsoleWrite("Starting tests" & @CRLF)

; Test 1
$aTemp = $aTemplate
$iTimer = TimerInit()
_ArrayRandomDelete($aTemp)
ConsoleWrite("Test 1 >>> " & TimerDiff($iTimer) & @CRLF)

; Test 2
$aTemp = $aTemplate
$iTimer = TimerInit()
_ArrayRandomShuffle1($aTemp)
ConsoleWrite("Test 2 >>> " & TimerDiff($iTimer) & @CRLF)

; Test 3
$aTemp = $aTemplate
$iTimer = TimerInit()
_ArrayRandomShuffle2($aTemp)
ConsoleWrite("Test 3 >>> " & TimerDiff($iTimer) & @CRLF)

Func _ArrayRandomDelete($aArray)
    For $i = $aArray[0] To 2 Step -1
        $iRand = Random(1, $aArray[0], 1)
        _ArrayDelete($aArray, $iRand)
        $aArray[0] -= 1
    Next
    _ArrayDelete($aArray, 1)
EndFunc   ;==>_ArrayRandomDelete

Func _ArrayRandomShuffle1(ByRef $aArray)
    Local $iRandom, $iBound = UBound($aArray) - 1
    For $i = 0 To $iBound
        $iRandom = Random(0, $iBound, 1)
        If $i <> $iRandom Then _ArraySwap($aArray[$i], $aArray[$iRandom])
    Next
EndFunc   ;==>_ArrayRandomShuffle1

Func _ArrayRandomShuffle2(ByRef $aArray)
    Local $iRandom, $swap, $iBound = UBound($aArray) - 1
    For $i = 0 To $iBound
        $iRandom = Random(0, $iBound, 1)
        If $i <> $iRandom Then
            $swap = $aArray[$i]
            $aArray[$i] = $aArray[$iRandom]
            $aArray[$iRandom] = $swap
        EndIf
    Next
EndFunc   ;==>_ArrayRandomShuffle2

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0