cookiemonster

Shuffle string

13 posts in this topic

Hoping for some guidance, I have a string like:

$string='vXx2586578£&'

How can I go abouts shuffling the contents of the string?

Share this post


Link to post
Share on other sites



8 hours ago, czardas said:
#include <Array.au3>

Local $string = 'vXx2586578£&'
Local $aChars = StringSplit($string, "", 2)

_ArrayShuffle($aChars)

Local $sNewString
For $i = 0 To UBound($aChars) - 1
    $sNewString &= $aChars[$i]
Next
MsgBox(0, "New String", $sNewString)

 

Works perfectly, I had tried doing it this way but messed part of it up, thanks for clarification.

1 person likes this

Share this post


Link to post
Share on other sites

Here the non array version:

Global $string='1234567890'

ConsoleWrite(ShuffleChars($string) & @CRLF)

Func ShuffleChars($sString, $iRounds = 1)
    Local $sTemp1, $sTemp2, $i, $j, $r, $t, $iLen, $rnd, $sLeft, $sMid, $sRight
    $iLen = StringLen($sString)
    For $r = 1 To $iRounds
        For $i = 1 To $iLen
            $j = $i
            $rnd = Random(1, $iLen, 1)
            If $rnd = $i Then ContinueLoop
            If $j < $rnd Then
                $t = $j
                $j = $rnd
                $rnd = $t
            EndIf
            $sTemp1 = StringMid($sString, $rnd, 1)
            $sTemp2 = StringMid($sString, $j, 1)
            $sLeft = StringMid($sString, 1, $rnd - 1)
            $sMid = StringMid($sString, $rnd + 1, $j - $rnd - 1)
            $sRight = StringMid($sString, $j + 1)
            $sString = $sLeft & $sTemp2 & $sMid & $sTemp1 & $sRight
        Next
    Next
    Return $sString
EndFunc

 


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

meant to post this here not in JG's example

-with powershell

#include<AutoItConstants.au3>
#include <Array.au3>

$Str = "shuffling"

$iPID = run("powershell -join ('" & _ArrayToString(stringsplit($Str , "" , 2) , "','") & "' | Get-Random -Count " & StringLen($Str) & ")" , "" , @SW_HIDE , $stdout_child)

$sOutput = ""

     While 1
        $sOutput &= StdoutRead($iPID)
        If @error Then ExitLoop
        WEnd

msgbox(0, '' , $sOutput)

 


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

AutoIt is like Burger King, you get so many great ways of doing things, you can have it your way :D

1 person likes this

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
12 minutes ago, JLogan3o13 said:

 

AutoIt is like Burger King

 

Bad taste :puke:

 

;)


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

The one time i would have used the delete button properly...


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

Hello I did this just for fun. it's based on swapping.

 

Local $string = 'vXx2586578£&'

ConsoleWrite(_StringShuffle($string) & @CRLF)
ConsoleWrite(_StringShuffle($string) & @CRLF)

Func _StringShuffle($sString)
Local $iRnd1 = 0
Local $iRnd2 = 0
Local $sChar1 = ""
Local $sChar2 = ""
Local $iMax = StringLen($sString)
For $i = 1 To $iMax
    $iRnd1 = Random(1, $iMax, 1)
    $iRnd2 = Random(1, $iMax, 1)
    $sChar1 = StringMid($sString, $iRnd1, 1)
    $sChar2 = StringMid($sString, $iRnd2, 1)
    $sString = StringReplace($sString, $iRnd1, $sChar2)
    $sString = StringReplace($sString, $iRnd2, $sChar1)
Next
Return $sString
EndFunc

Saludos

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

  • Similar Content

    • cheeroke
      By cheeroke
      Hi all,
      I got this code and would like to be able to change Baud Rate and instead of sending character by character i would like to be able (if possible) to send whole string. But i don't know how to change it.
      I am taking input from file and processing whole line (this is done in FilesHandling.au3).
      To execute this i am just calling SendData("FileName", int) in "main" script.
      Any help very appreciated.
      #include <WinAPI.au3> #include <Array.au3> #include "FilesHandling.au3" ;init DLL function, we need handle to call the function $h = DllCall("Kernel32.dll", "hwnd", "CreateFile", "str", "\\.\COM19", "int", BitOR($GENERIC_READ,$GENERIC_WRITE), "int", 0, "ptr", 0, "int", $OPEN_EXISTING, "int", $FILE_ATTRIBUTE_NORMAL, "int", 0) $handle=$h[0] Func SendData($FileName, $LineNumber) ;string to be send $c = readFile($FileName, $LineNumber) $cLenght = StringLen($c) $aArray = StringSplit($c, "") ;_ArrayDisplay($aArray, "", Default, 64) For $i = 1 To $cLenght writeChar($handle, $aArray[$i], $cLenght) Next ;move to next line writeChar($handle, @CR,1) EndFunc ;write a single char func writeChar($handle,$c,) $stString = DLLStructCreate("char str") $lpNumberOfBytesWritten = 0 DllStructSetData($stString, 1, $c) $res = _WinAPI_WriteFile($handle, DllStructGetPtr($stString, "str"), 1,$lpNumberOfBytesWritten) if ($res<>true) then ConsoleWrite ( _WinAPI_GetLastErrorMessage() & @LF) EndIf EndFunc  
    • FroVN
      By FroVN
      Hi, i have a problem :" can't set the name of file with a special character like: \;/;";|;...  have anyway to short the StringInSrt and Stringreplace? i am using this code but too long
      $title=InputBox(0,'','','')
         if StringInStr($title,'\') or StringInStr($title,'/') or StringInStr($title,':') or StringInStr($title,'*') or StringInStr($title,'?') or StringInStr($title,'"') or StringInStr($title,'<') or StringInStr($title,'>') or StringInStr($title,'|') Then
             $title=StringReplace($title,'\','-')
              $title=StringReplace($title,'/','-')
               $title=StringReplace($title,':','-')
                $title=StringReplace($title,'*','-')
                 $title=StringReplace($title,'?','-')
                  $title=StringReplace($title,'"','-')
                   $title=StringReplace($title,'<','-')
                    $title=StringReplace($title,'>','-')
                     $title=StringReplace($title,'|','-')
         EndIf
       
    • JustinZandee
      By JustinZandee
      I need a random string generator which creates 15 letters/numbers.
      How can I make that?
    • Anas
      By Anas
      Hi,
      I've this script (removed unrelated parts) that generate a set of random chars for a password. It gets launched from a network share by Task Scheduler at a specific time for all PC's, then it relaunches itself from the local drive.
       
      Global $LocalToolsDir = @ProgramFilesDir & '\Tools' Start() Func Start() If Not StringInStr(@ScriptFullPath, $LocalToolsDir, 2) Then Exit RunLocaly() Sleep(Random(1, 1800, 1) * 1000) $NewPass = GeneratePass() EndFunc Func GeneratePass() Local $Pass For $i = 1 to 8 $R = Random(0,1.5) If $R > 1 Then $Chr = Random(0,9,1) ElseIf $R < 0.5 Then $Chr = Chr(Random(Asc("A"), Asc("Z"), 1)) Else $Chr = Chr(Random(Asc("a"), Asc("z"), 1)) Endif $Pass &= $Chr Next Return $Pass EndFunc Func RunLocaly() Local $Run = 1, $LocalApp = $LocalToolsDir & '\' & @ScriptName If Not FileExists($LocalApp) Then $Run = FileCopy(@ScriptFullPath, $LocalToolsDir, 1 + 8) If $Run Then Run($LocalApp, $LocalToolsDir) EndFunc  
      The problem is that many PC's ends up with the same password(s).
      e.g. 10 PC's have (abc123) as a Password, and another 10 have this (def456)
      Any idea why?