Shuffle (Randomize) letters in a word. - Forum

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

Shuffle (Randomize) letters in a word.

Page 1 of 2Next

Hi, does anyone know of a plugin or code that can shuffle (randomize) letters in a word?

Example: If i type the word "tomorrow" in a TextEntry and click a button to shuffle (randomize) the word, it must look something like this: "orwmtoro

Thank You!

https://stackoverflow.com/questions/46477781/how-to-scramble-characters-in-a-string

 

my 0.01

 

@impactband4u

Here's a version of the code that does this.

 

Uploaded files:
  • You need to login to have access to uploads.
luishp, impactband4u and 3 other users have reacted to this post.
luishpimpactband4uCDY@44josevdr95emo

Thank you guys!

@vadim - Thank you for the sample. Exactly what I needed : )

@impactband4u

Happy to help!

I forgot to remind you that you can remove the ListBox1 object from the page (for example, place it on the left side of the page) if you don't want to show the process and only need the mixing result.

impactband4u has reacted to this post.
impactband4u

I believe this will work:

SetVar "[NewText]" ""
StrLen "[TextEntry1]" "[Length]"
Loop "1" "[Length]" "[LengthLoop]"
StrLen "[TextEntry1]" "[NewLength]"
Random "[NewLength]" "[RandNum]"
If "[RandNum]" "=" "0"
SetVar "[RandNum]" "1"
EndIf
SubStr "[TextEntry1]" "[RandNum]" "1" "[RandChar]"
StrDel "[TextEntry1]" "[RandNum]" "1" "[TextEntry1]"
SetVar "[NewText]" "[NewText][RandChar]"
EndLoop

 

Vadim has reacted to this post.
Vadim

Edited later :

PLEASE IGNORE THIS POST ... I mistakenly thought it was an enquiry for VisualNEOWeb

@impactband4u
@vadim
@pauljonestindall

There is a native VisualNEOWeb command for doing this ... only, it is for shuffling items inside an array ... so you need to parse the string variable before deploying the command ... and combine the shuffled array items into a string afterwards ... this works ...

SetVar "[OriginalText]" "typewriter"

... make array of one character each
StrParse "[originalText]" "" "[originalArray]"

... do the shuffle
CreateEmptyArray [shuffledArray]
ArrayShuffle [originalArray] [shuffledArray]

... merge shuffled array items into a text variable
ArrayLen [shuffledArray] [shuffledArrayLen]
Math "[shuffledArrayLen]-1" 0 [shuffledArrayMax]

SetVar [shuffledText] ""
Loop 0 [shuffledArrayMax] [thisShuffledArrayItem]
SetVar "[thisShuffledItemText]" "[shuffledArray([thisShuffledArrayItem])"
SetVar [shuffledText] "[shuffledText][thisShuffledItemText]"
EndLoop

AlertBox "shuffledText" "[shuffledText]" ""

Note that Javascript has a .join() method for arrays that would reduce the 'array to text string' code ... but I could not find a corresponding VisualNEOWeb command.

@pauljonestindall

It works great! Your code came out much shorter! Thanks for sharing!

@gaev

Glad to know that VisualNEO Web has such a specialized command! Thank you!

@impactband4u
@vadim
@gaev

Unfortunately the StrParse doesn't work in NVWin the same as NVWeb. I've tried using nothing as a delimiter before.
I figured the easiest way was to just remove the characters from the original string that were already selected. And repeat.

@pauljonestindall

In my project VisualNEO Win command StrParse works fine with different delimiters, in particular I use semicolon and equal sign.

@vadim

In @gaev example for NVWeb the StrParse action has nothing as a delimiter, or, has no delimiter.

StrParse "[originalText]" "" "[originalArray]"

This action command would not work in NVWin. In this exercise, you can't have nothing as a delimiter.

@pauljonestindall

And in this problem, yes, this command will not help. I got it wrong at first. I thought we were talking about the functionality of the command ))
Translation difficulties. Sorry.

@impactband4u
@vadim
@pauljonestindall

Sorry guys, I messed up big time ... I was thinking this was a query in VisualNEOWeb.

Excuses for this brain freeze ...

- it was late in the night over here
- we have had two weeks of extreme cold and snow
- add to it, two years of self isolation related to covid
- could even be a new strain of covid

... and I am sticking to them.

I will edit my original post to reflect this messup.

@gaev

That's okay. Your message made it clear from the start that you were talking about VisualNEO Web. It is useful to know if there are any differences in the syntax of these programming environments.

@gaev
@vadim

Vadim is correct. I haven't fully explored NVW a whole lot, but it is interesting to know and have more of the extra features and action commands like the ArrayShuffle and being able to parse by nothing. It does however make the two platforms a bit more dissimilar than I would like.

@vadim

It seems to me that instead of the If...Then statement to change a random number of zero to one, you could use a StrReplace action.
For example:

SetVar "[NewText]" ""
StrLen "[TextEntry1]" "[Length]"
Loop "1" "[Length]" "[LengthLoop]"
StrLen "[TextEntry1]" "[NewLength]"
Random "[NewLength]" "[RandNum]"
StrReplace "[RandNum]" "0" "1" "[RandNum]" ""
.If "[RandNum]" "=" "0"
.SetVar "[RandNum]" "1"
.EndIf
SubStr "[TextEntry1]" "[RandNum]" "1" "[RandChar]"
StrDel "[TextEntry1]" "[RandNum]" "1" "[TextEntry1]"
SetVar "[NewText]" "[NewText][RandChar]"
EndLoop

It works most every time, but it doesn't work every time.

@pauljonestindall

Yes, thanks for the idea, that's an option too! Quite workable!

@vadim

But it doesn't work every single time and I cannot figure out why.

@pauljonestindall

I've checked several times, it works great! At what point do you notice the error? What exactly is not working?

@vadim

I'm including my test PUB. I added two lines just to get the information, one to capture any [LastError] and one to confirm the "shuffled" text length.

If you enter 0123456789 in the text entry, copy it to the clipboard and hit enter it goes through the process. paste the 0123456789 into the text entry and hit enter... repeat this several times...

I never get an error but just some times it will leave the last character in the entry box. Not always the same character and not always after the same number of repeats.

I know this may seem like a rabbit hole and you may be wondering how I found this out but the fact is it doesn't always work correctly and that's a bit concerning.

Uploaded files:
  • You need to login to have access to uploads.
Page 1 of 2Next