Jump to content

Random Array


Somerset
 Share

Recommended Posts

Dim $avArray[10],$z = ""
$avArray[0] = "A"
$avArray[1] = "B"
$avArray[2] = "C"
$avArray[3] = "D"
$avArray[4] = "E"
$avArray[5] = "F"
$avArray[6] = "G"
$avArray[7] = "H"
$avArray[8] = "I"
$avArray[9] = "J"
;random array
For $a = 0 to 9
$b=Random (0,9,1 )
if $avArray[$b] = "" then $a = $a - 1
if $avArray[$b] <> "" then $z = $z & $avArray[$b]
$avArray[$b]=""
Next
MsgBox(0, "Array", $z)

You would need string split to return it to an array, simple enough.

Link to comment
Share on other sites

Nice script, this could be used to create a random password

Edited by RazerM
My Programs:AInstall - Create a standalone installer for your programUnit Converter - Converts Length, Area, Volume, Weight, Temperature and Pressure to different unitsBinary Clock - Hours, minutes and seconds have 10 columns each to display timeAutoIt Editor - Code Editor with Syntax Highlighting.Laserix Editor & Player - Create, Edit and Play Laserix LevelsLyric Syncer - Create and use Synchronised Lyrics.Connect 4 - 2 Player Connect 4 Game (Local or Online!, Formatted Chat!!)MD5, SHA-1, SHA-256, Tiger and Whirlpool Hash Finder - Dictionary and Brute Force FindCool Text Client - Create Rendered ImageMy UDF's:GUI Enhance - Enhance your GUIs visually.IDEA File Encryption - Encrypt and decrypt files easily! File Rename - Rename files easilyRC4 Text Encryption - Encrypt text using the RC4 AlgorithmPrime Number - Check if a number is primeString Remove - remove lots of strings at onceProgress Bar - made easySound UDF - Play, Pause, Resume, Seek and Stop.
Link to comment
Share on other sites

Here's a cleaner way:

#Include <Array.au3>
Dim $s_Out, $a_Chars = StringSplit('ABCDEFGHIJ','')
For $i_Count = 1 To $a_Chars[0]
    _ArraySwap($a_Chars[$i_Count],$a_Chars[Random(1,$a_Chars[0],1)])
Next
For $i_Count = 1 To $a_Chars[0]
    $s_Out &= $a_Chars[$i_Count]
Next
MsgBox(0, "Array", $s_Out)
Link to comment
Share on other sites

Here's a cleaner way:

#Include <Array.au3>
Dim $s_Out, $a_Chars = StringSplit('ABCDEFGHIJ','')
For $i_Count = 1 To $a_Chars[0]
    _ArraySwap($a_Chars[$i_Count],$a_Chars[Random(1,$a_Chars[0],1)])
Next
For $i_Count = 1 To $a_Chars[0]
    $s_Out &= $a_Chars[$i_Count]
Next
MsgBox(0, "Array", $s_Out)
cleaner way? :)
Link to comment
Share on other sites

cleaner way? :)

For a few reasons:

Your routine makes it difficult to add or remove characters from the random pool. You have to edit all those lines manually.

And this line:

if $avArray[$b] = "" then $a = $a - 1

is "bad" for 2 reasons:

Modifying the loop control variable is a programming no-no

It is very likey that your loop will run more than 10 iterations. In fact, the bigger the inital array is, the more hit-and-miss you'll have, so this routine will be exponentially slower as the array size increases.

Here's an even better routine than my last one:

#Include <Array.au3>
Dim $s_Out, $a_Chars = StringSplit('ABCDEFGHIJ','')
For $i_Count = 1 To $a_Chars[0]
    _ArraySwap($a_Chars[$i_Count],$a_Chars[Random($i_Count,$a_Chars[0],1)])
    $s_Out &= $a_Chars[$i_Count]
Next
MsgBox(0, "Array", $s_Out)

I changed the random call so that it only picks unused characters from the "rest" of the array, so there is no need to loop through it twice.

Link to comment
Share on other sites

:) hey silly ass the reason for it was just to put the idea out there. How the string/array is entered into it pointless, just showing it was possible to do it. Now try to do it without calling _array calls, once that has been done you just created a new function... :mellow:
Link to comment
Share on other sites

:) hey silly ass the reason for it was just to put the idea out there. How the string/array is entered into it pointless, just showing it was possible to do it. Now try to do it without calling _array calls, once that has been done you just created a new function... :mellow:

Func _StringScramble($s_In)
  Dim $a_In = StringSplit($s_In, ''), $i_Count, $i_Index, $s_Temp, $s_Out=''
  For $i_Count = 1 to $a_In[0]
    $s_Temp = $a_In[$i_Count]
    $i_Index = Random($i_Count, $a_In[0], 1)
    $a_In[$i_Count] = $a_In[$i_Index]
    $a_In[$i_Index] = $s_Temp
    $s_Out &= $a_In[$i_Count]
  Next
  Return $s_Out
EndFunc

MsgBox(0,@ScriptName,_StringScramble('ABCDEFGH'))
Link to comment
Share on other sites

wtg :)

question should you add a varible to string split just incase the end user wish to have array elements larger than just one character?

Func _StringScramble($s_In,$s_delimiter)

Dim $a_In = StringSplit($s_In, $s_delimiter), $i_Count, $i_Index, $s_Temp, $s_Out=''

For $i_Count = 1 to $a_In[0]

$s_Temp = $a_In[$i_Count]

$i_Index = Random($i_Count, $a_In[0], 1)

$a_In[$i_Count] = $a_In[$i_Index]

$a_In[$i_Index] = $s_Temp

$s_Out &= $a_In[$i_Count]

Next

Return $s_Out

EndFunc

MsgBox(0,@ScriptName,_StringScramble('ABCDEFGH'))

Edited by beerman
Link to comment
Share on other sites

  • 1 year later...

Func _StringScramble($s_In,$s_Inde,$s_Outde)
  Dim $a_In = StringSplit($s_In, $s_Inde), $i_Count, $i_Index, $s_Temp, $s_Out=''
  For $i_Count = 1 to $a_In[0]
    $s_Temp = $a_In[$i_Count]
    $i_Index = Random($i_Count, $a_In[0], 1)
    $a_In[$i_Count] = $a_In[$i_Index]
    $a_In[$i_Index] = $s_Temp
    $s_Out &= $a_In[$i_Count]&$s_Outde
  Next
  Return stringtrimright($s_Out,1)
EndFunc
oÝ÷ Ù(§qæåwp  Þ½êëzÊh×q©ì¢g¨ævØb²Ú-ë|uçl¢gzX¦×«²Ú!zZl¢Ø^{-®)àq©Ûz§vWrëyËeÉìZ^ºÇ°¢é]m殶­s`¤gVæ2õ7G&æu67&Ö&ÆRb33c·5ôâÂb33c·5ôæFRÂb33c·5ô÷WFFR¢FÒb33c¶ôâÒ7G&æu7ÆBb33c·5ôâÂb33c·5ôæFRÂb33c¶ô6÷VçBÂb33c¶ôæFWÂb33c·5õFV×Âb33c·5ô÷WCÒb33²b33°¢f÷"b33c¶ô6÷VçBÒFòb33c¶ôå³Ð¢b33c·5õFV×Òb33c¶ôå²b33c¶ô6÷VçEТb33c¶ôæFWÒ&æFöÒb33c¶ô6÷VçBÂb33c¶ôå³Ò¢b33c¶ôå²b33c¶ô6÷VçEÒÒb33c¶ôå²b33c¶ôæFWТb33c¶ôå²b33c¶ôæFWÒÒb33c·5õFV×¢b33c·5ô÷WBf׳Òb33c¶ôå²b33c¶ô6÷VçEÒfײb33c·5ô÷WFFP¢æW@¢&WGW&â7G&æwG&×&vBb33c·5ô÷WBäVæDgVæ0 ¢b33c¶Õõ7G&æu67&Ö&ÆRb33´Ô#"Ô32ÔCBÔSRÔcbÔsrÔb33²ÂgV÷C²ÒgV÷C²ÂgV÷C³¢gV÷C²¤×6t&÷Ä67&DæÖRÂb33c¶

the resulting out would be that same string but randomize, with the new delimiter set by the user, ready for stringsplit usage... :shocked:

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...