Jump to content

_StringShuffle


jguinch
 Share

Recommended Posts

Here is a small function to shuffle a string :

ConsoleWrite( _StringShuffle("I like AutoIt !") )
 
Func _StringShuffle($sString)
    For $i = StringLen($sString) - 1 To 0 Step -1
        $sString = StringRegExpReplace($sString, "(?s)(.{" & Random(0, $i, 1) & "})(.)(.*)", "$1$3$2")
    Next
    Return $sString
EndFunc

It could be useful ?

Link to comment
Share on other sites

Nice approach using regex.  :thumbsup:

 

Here the array version additionally:

#include <Array.au3>

$sText = "_ArrayShuffle shuffles selected rows of 1D or 2D arrays - can be limited to a specific column in 2D arrays."

$t = TimerInit()
ConsoleWrite( _StringShuffle($sText) & @CRLF )
ConsoleWrite(TimerDiff($t) & @CRLF & @CRLF)

$t = TimerInit()
ConsoleWrite( _StringShuffle2($sText) & @CRLF )
ConsoleWrite(TimerDiff($t) & @CRLF)


Func _StringShuffle($sString)
    For $i = StringLen($sString) - 1 To 0 Step -1
        $sString = StringRegExpReplace($sString, "(?s)(.{" & Random(0, $i, 1) & "})(.)(.*)", "$1$3$2")
    Next
    Return $sString
EndFunc

Func _StringShuffle2($sString)
    Local $a = StringSplit($sString, "", 2)
    _ArrayShuffle($a)
    Return _ArrayToString($a, "")
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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Very nice.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

if _stringshuffle --> stringsplit is faster than stringsplit --> _arrayshuffle

and if there is a way you could add a parameter for a delimeter (e.g. so you only shuffle the things between commas, such that the string can be still be split on the commas afterward and result with elements rearranged).  I could see that being used widely.

Edited by boththose

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

Link to comment
Share on other sites

What about this array-based version ? Including separator option, using StringSplit and faster than _ArrayShuffle :

#include <Array.au3>

$sText = "_ArrayShuffle shuffles ,selected rows of 1D or 2D arrays, - can be limited to a specific column in 2D arrays."

$t = TimerInit()
ConsoleWrite( _StringShuffle($sText) & @CRLF )
ConsoleWrite(TimerDiff($t) & @CRLF & @CRLF)

$t = TimerInit()
ConsoleWrite( _StringShuffle2($sText) & @CRLF )
ConsoleWrite(TimerDiff($t) & @CRLF)


Func _StringShuffle($sString, $sDelimiter = "")
    Local $aString = StringSplit($sString, $sDelimiter, 2)
    Local $sOutput

    Local $iUbound = UBound($aString) - 1

    For $i = 0 To $iUbound
        $iRand = ( $i = $iUbound ? $iUbound : Random($i, $iUbound, 1) )
        $iPrev = $aString[$iRand]
        $aString[$iRand] = $aString[$i]

        $sOutput &= $iPrev
    Next
    
    Return $sOutput

EndFunc

Func _StringShuffle2($sString)
    Local $a = StringSplit($sString, "", 2)
    _ArrayShuffle($a)
    Return _ArrayToString($a, "")
EndFunc
Edited by jguinch
Link to comment
Share on other sites

Well, in principle the _ArrayShuffle function for 1D arrays is the same as what you did. The difference is the overhead within the function.

That's the reason why I used it and it looks smaller than to have the loop in the function. ;)

Edited by UEZ

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Nice example jguinch. I was wondering if the technique could be applied to array syntax such as '[[1,2],[3,4]]'. I think it would be quite a challenge but also a very neat trick. My (old) multidimensional shuffle seems to contain so much bloat and is currently limited to 11 dimensions. If anyone is interested it's here:

I remember it driving me crazy trying to figure it out. :whisper:

Edited by czardas
Link to comment
Share on other sites

  • 1 year later...

a sporting effort 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)

 

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

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

×
×
  • Create New...