Shuffle (Randomize) letters in a word. - Page 2 - Forum

Forum Navigation
You need to log in to create posts and topics.

Shuffle (Randomize) letters in a word.

PreviousPage 2 of 2

@pauljonestindall

Rewrites 10 to 11 because it contains zero.

Random "[NewLength]" "[RandNum]"

... if Random number 10

StrReplace "[RandNum]" "0" "1" "[RandNum]" ""

... then the random number here will be 11


@pauljonestindall

Thanks @iretz ! So it's still better to use a logical condition to bypass zero.

At the risk of messing up (again), hope you find this VisualNEOWin code useful.

It incorporates a trick I learned a long time ago, to work around the fact that Random works with a base of zero, while string positions use a base of 1 ... also avoids having to check for a randomized result of zero.

SetVar "[OriginalText]" "qwertyqwerty"
StrLen "[OriginalText]" "[OriginalTextLength]"


... copy to working text variable
SetVar "[thisText]" "[OriginalText]"
StrLen "[thisText]" "[thisTextLength]"

SetVar "[RandomizedText]" ""

Loop "1" "[OriginalTextLength]" "[thisLoop]"

... get current string length
StrLen "[thisText]" "[thisTextLength]"

... get random number between zero and 'text length minus 1'
Random "[thisTextLength]-1" "[thisRandomNumber]"
... recalibrate for base 0
Math "[thisRandomNumber]+1" "0" "[thisTextPosition]"

... extract character
SubStr "[thisText]" "[thisTextPosition]" "1" "[thisExtractedCharacter]"

... append to [RandomizedText]
SetVar "[RandomizedText]" "[RandomizedText][thisExtractedCharacter]"

... remove from [thisText]
StrDel "[thisText]" "[thisTextPosition]" "1" "[thisText]"

...AlertBox "Trace" "[thisLoop]|[thisTextLength]|[thisTextPosition]|[thisExtractedCharacter]|[RandomizedText]"

EndLoop

AlertBox "Randomized Text" "[RandomizedText]"

It works for me; let me know if your experience is different.

@gaev

It works perfectly! Great trick with the zero bypass!!! That hadn't occurred to me! Thanks for sharing!!! Very interesting!!!

@vadim

For those developers who need to do the base conversion often, I suggest making up a Function that can then be Call'ed e.g.

Call "RandomBase1" "[MaxNumber]" "[RandomResult]"

 

@vadim

Yes, I see. If the very first random number generated is 10, the zero is changed to 1, making the number 11. the is no character in the eleventh position to read, no character to delete and therefore the length doesn't change through the next loop leaving one unread character in the original text entry.

You have to use if...then rather than StrReplace.

PreviousPage 2 of 2