Shuffler characters

I'm just wondering if there is a way (using ASP.NET C #) to “shuffle” the contents of the string, but still you can press another button and “unshuffle” back to the original content without saving the original content?

Thank:)

Example:

"This is not shuffled."

"isuo .tffsnl iTh shed"

...And then I click the "UNShuffle" button and it becomes normal again:

"This is not shuffled."
+3
source share
2 answers

Well, you need to save something. One simple idea:

  • Use the random number generator to generate random seed.
  • Create a new instance Randomto use for shuffling using this seed
  • Shuffle with a modified Fisher-Yates shuffle
  • Save the seed

- , . ( , , 0... (n-1) , .)

, - . , , , , "abc" "bac" "cab".

+6

:

var rnd = new Random();
string unsuffled = "This is not shuffled.";
string shuffled = new string(unsuffled.OrderBy(r => rnd.Next()).ToArray());

, , .

+8

Source: https://habr.com/ru/post/1754197/


All Articles