E
E
estry2018-08-21 12:04:07
C++ / C#
estry, 2018-08-21 12:04:07

How to randomize a string?

Hey!
How to randomize a string in seasharp?
I'll show you with an example.
There is a line: toster cool
You need to get as many variations as possible.
Result: toSsster Cooool, ttosterr-ccoool, etc.
How to do it? Thanks for the tip

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
phoboson, 2018-08-21
@estry

If I understood correctly:

public static string RandomizeString(string str)
        {
            var chars = str.ToArray();
            var random = new Random();
            for(int n = chars.Length-1; n>=0; n--)
            {
                var randomNumber = random.Next(chars.Length);
                var savedChar = chars[randomNumber];
                chars[randomNumber] = chars[n];
                chars[n] = savedChar;
            }
            return new string(chars);
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question