TjW Posted March 7, 2015 Share Posted March 7, 2015 For $i = 0 To 3 ^I am using to go through and select each one, I would like it to randomly choose a variable of 0, 1, 2, 3 but not stop until all 4 and tried as $i This just goes in order 0, 1, 2, 3 Dim $test[4] for $i = 0 to 3 $test[$i] = "Hello i am " & $i Next $test[0] = UBound($test) $r = Random(1,$test[0]) $m=$test[$r] msgbox (0,"this is $m:",$m & " The random was: " & $r) This does choose random but don't know how to make it choose randomly and eliminate number "x" as I go. This can be done completely different if need be as well, any help appreciated Link to comment Share on other sites More sharing options...
kylomas Posted March 7, 2015 Share Posted March 7, 2015 TjW, Here's one way... #include <array.au3> local $aValues = ['water', 'earth', 'fire', 'ice'], $idx while ubound($aValues) $idx = random(0, ubound($aValues) - 1,1) ConsoleWrite($aValues[$idx] & @CRLF) _arraydelete($aValues,$idx) wend kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
water Posted March 7, 2015 Share Posted March 7, 2015 Welcome to AutoIt and the forum! Could you please do yourself and us a favour and enclose your script in AutoIt code tags? That's the blue "A" icon in the editor. This formats and syntax highlights the code and greatly enhances readability (as you cen see in post #2 by kylomas) My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
TjW Posted March 7, 2015 Author Share Posted March 7, 2015 Welcome to AutoIt and the forum! Could you please do yourself and us a favour and enclose your script in AutoIt code tags? That's the blue "A" icon in the editor. This formats and syntax highlights the code and greatly enhances readability (as you cen see in post #2 by kylomas) Not a problem Link to comment Share on other sites More sharing options...
nullschritt Posted March 8, 2015 Share Posted March 8, 2015 (edited) I believe you're looking for something like this:#include <array.au3> Dim $test[4] dim $lastnumber[4] for $i = 0 to 3 $test[$i] = "Hello i am " & $i Next dim $max = UBound($test)-1 for $i = 0 to 3 $r = StringReplace(Round(Random(-1,$max)), "-1", "0") while _ArraySearch($lastnumber, $r) <> -1 $r = StringReplace(Round(Random(-1,$max)), "-1", "0") WEnd $m=$test[$r] $lastnumber[$i] = $r msgbox (0,"this is $m: "&$r, " The random was: " & $m) Next _ArrayDisplay($lastnumber)Note: This has a non terminating while loop in it that will keep guessing random numbers until it guesses one that hasn't been guessed, in certain instances this can take a VERY long time, but it's only "frozen" once out of the 10 times I tested it, so I don't know if that is going to be a problem in all application or if the thread just froze on me. Edited March 8, 2015 by nullschritt Link to comment Share on other sites More sharing options...
nullschritt Posted March 8, 2015 Share Posted March 8, 2015 (edited) I wrote this as a test of the functionality and it works flawlessly every time#include <array.au3> $timer = TimerInit() $time = 0 for $i2=1 to 100 Dim $test[4] dim $lastnumber[4] for $i = 0 to 3 $test[$i] = "Hello i am " & $i&" |" Next dim $max = UBound($test)-1 for $i = 0 to 3 $r = StringReplace(Round(Random(-1,$max)), "-1", "0") while _ArraySearch($lastnumber, $r) <> -1 $r = StringReplace(Round(Random(-1,$max)), "-1", "0") WEnd $m=$test[$r] $lastnumber[$i] = $r ConsoleWrite($m) Next $Time += timerdiff($timer)/1000 ConsoleWrite(@CRLF) ConsoleWrite("Order "& _arraytostring($lastnumber, ",")&@CRLF) $timer = TimerInit() Next ConsoleWrite("Time Average: "&$time/100&" Seconds"&@CRLF)Note: I'm using -1 as 0 because for some reason random seems to almost NEVER pick 0.Edit: Updated code to measure speed. Seems to take about 1/720th of a second to guess the 4 random numbers with no repeats. But you can use as many values as you want, I tested it with 200 and it took 0.110866811824646 Seconds or about 1/10th of a second on average. Edited March 8, 2015 by nullschritt Link to comment Share on other sites More sharing options...
jchd Posted March 8, 2015 Share Posted March 8, 2015 _ArrayShuffle is your best friend here: exactly 1 line of code. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
jguinch Posted March 8, 2015 Share Posted March 8, 2015 (edited) If you want to avoid (why not ?) to include Array.au3, you can just do something like this : Local $aNumbers[10] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] For $i = 0 To UBound($aNumbers) - 1 $iRand = ( $i = UBound($aNumbers) - 1 ? UBound($aNumbers) - 1 : Random($i, UBound($aNumbers) - 1, 1) ) $iPrev = $aNumbers[$iRand] $aNumbers[$iRand] = $aNumbers[$i] $aNumbers[$i] = $iPrev ConsoleWrite($aNumbers[$i] & @CRLF) Next _ArrayDisplay($aNumbers) An other scatty way (i found it funny) without using an array (if you want just a few change in your existing code) : Local $iMin = 0, $iMax = 10 For $i = $iMin To $iMax $iN = _RandomLoop($iMin, $iMax) ConsoleWrite($iN & @CRLF) Next Func _RandomLoop($iStart, $iEnd) Local Static $sShuffle, $iCount, $iStartPrev, $iEndPrev If $iStartPrev <> $iStart Or $iEndPrev <> $iEnd Then $iStartPrev = $iStart $iEndPrev = $iEnd $iCount = $iEnd - $iStart + 1 $sShuffle = "" For $i = $iStart To $iEnd ; build the String in order $sShuffle &= $i & "," Next For $i = $iCount To 0 Step -1 ; shuffle numbers in the string $sShuffle = StringRegExpReplace($sShuffle, "((?:-?\d+,){" & Random(0, $i, 1) & "})(-?\d+,)(.*)", "$1$3$2") Next EndIf Local $iVal = StringRegExpReplace($sShuffle, ",[-\d]*", "") $sShuffle = StringRegExpReplace($sShuffle, "^-?\d+,", "") Return $iVal <> "" ? Number($iVal) : SetError(1, 0, "") ; @error = 1 if all numbers have been chosen and return is blank EndFunc (just an unnecessary idiocy I think ...) Edited March 9, 2015 by jguinch Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
nullschritt Posted March 8, 2015 Share Posted March 8, 2015 @jguinch your scripts return the same value more than once. He wants each value to only be selected once. ( : (So you need to keep track of the previous selections) Link to comment Share on other sites More sharing options...
jguinch Posted March 9, 2015 Share Posted March 9, 2015 @nullschritt : you're right. My ConsoleWrite did not write the good variable. It's edited now Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
jchd Posted March 9, 2015 Share Posted March 9, 2015 Guess what: #include <array.au3> Local $aNumbers[10] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] _ArrayShuffle($aNumbers) _ArrayDisplay($aNumbers) This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
iamtheky Posted March 9, 2015 Share Posted March 9, 2015 my no array attempt: Local $aNumbers[10] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] $z = 0 While 1 $i = random(0, ubound($aNumbers) - 1 , 1) If $aNumbers[$i] = "" Then continueloop Else consolewrite($aNumbers[$i] & @CRLF) $aNumbers[$i] = "" $z += 1 If $z = ubound($aNumbers) Then exitloop EndIf Wend ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) 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