List<char> inputChars = new List<char>(input);
char[] outputChars = new char[inputChars.Count];
Random rand = new Random();
// fill backwards to simplify Rand.Next() logic
for (int i = inputChars.Count - 1; i >= 0; i--)
{
int index = rand.Next(i);
outputChars = inputChars[index];
inputChars.RemoveAt(index);
}
return new string(outputChars);
}
public static void Main()
{
string foo = "The quick brown fox jumps over the lazy
dog";
string bar = Scramble(foo);
}
namespace E
{
public class Program
{
private static Random rng = new Random();
public static void Main(string[] args)
{
string s = "Hello world !";
char[] c = s.ToCharArray();
byte[] b = new byte[c.Length];
rng.NextBytes(b);
Array.Sort<byte, char>(b, c);
string s2 = new string(c);
Console.WriteLine(s2);
Console.ReadKey();
}
}
}
It is not the most efficient method, but the code is simple.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.