playMaker

Author Topic: Shuffle FsmString  (Read 1872 times)

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 808
Shuffle FsmString
« on: April 27, 2021, 12:49:05 PM »
There's this piece of code that looks appetizing, it's providing a way to quickly scramble a string variable.

Code: [Select]
public static string  Shuffle(this string str)
    {
        char[] array = str.ToCharArray();
        Random rng = new Random();
        int n = array.Length;
        while (n > 1)
        {
            n--;
            int k = rng.Next(n + 1);
            var value = array[k];
            array[k] = array[n];
            array[n] = value;
        }
        return new string(array);
    }

Unfortunately it's providing an extension method and must be defined inside a static non-generic class. Can it be modified to be integrated into a single stand-alone action script?

With the following description?

Quote
"Randomizes the characters within a FsmString variable. You might want to clean up the result with other actions in the case you used white spaces that might end at the beginning or the ending of your scrambled string variable."
« Last Edit: April 29, 2021, 06:04:04 AM by Broken Stylus »