sigil Posted July 8, 2011 Posted July 8, 2011 I'm trying to make a function that scrambles a string (puts its characters in random order). But for some reason, my function will change the contents of the string, resulting in extra copies of the same character in my output string. Here's my test code: #include <misc.au3> #include <array.au3> $word="HELLO" $newWord=scramble($word) msgbox(0,"",$newWord&" "&$word) Exit func scramble($str) local $temp=$str $tempArray=stringsplit($temp,"") dim $newArray[1] _arraydelete($tempArray,0) $tMax=ubound($tempArray) for $c=0 to $tMax-1 _arraydisplay($tempArray) $select=random(0,ubound($tempArray)-1) _arrayadd($newArray,$tempArray[$select]) _ArrayDelete($tempArray,$select) Next $newstr=_arraytostring($newArray,"") return $newStr EndFunc Why is it adding duplicate characters?
sigil Posted July 8, 2011 Author Posted July 8, 2011 I solved the problem. In the For loop, I needed to add a 1 flag to Random() so that it would generate integer values only.
MvGulik Posted July 8, 2011 Posted July 8, 2011 (edited) Don't know. But your code seems to trigger a _arrayadd() error on its last For-Next loop. Might be related. ;; add following debug #directive to top of your code. #AutoIt3Wrapper_Run_Debug_Mode=y ;; error result. ;; LINE: 1-0: _arrayadd($newArray,$tempArray[$select]) --- Oops ... to late. (argg, I always forget its the line above the one that displays the error. ) Edited July 8, 2011 by iEvKI3gv9Wrkd41u "Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions.""The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014) "Believing what you know ain't so" ... Knock Knock ...
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