I need to create a Dictionary that expresses a mapping between each char in an alphabet and another char in that alphabet, where both the key and value are unique -- like a very simple cipher that expresses how to code/decode a message. There can be no duplicate keys or values.
Does anyone see what is wrong with this code? It is still producing duplicate values in the mapping despite the fact that on each iteration the pool of available characters decreases for each value already used.
string source_alphabet = _alphabet; //ie "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
string target_alphabet = _alphabet;
Dictionary<char, char> _map = new Dictionary<char, char>();
for (int i = 0; i < source_alphabet.Length; i++)
{
int random = _random.Next(target_alphabet.Length - 1); //select a random index
char _output = target_alphabet[random] //get the char at the random index
_map.Add(source_alphabet[i], _output); //add to the dictionary
target_alphabet = target_alphabet.Replace(_output.ToString(), string.Empty);
// remove the char we just added from the remaining alphabet
}
Thanks.