0

I'm writing an alphabet cipher that ciphers at random. It looks like it should work when I'm debugging it but when I look at the cipher key it comes out with duplicate letters like some of them weren't switched during the for loop. Any help is much appreciated. WasSwapped is used to keep track of whether the letters been used in the cipher already and alphabet is just a list of the alphabet in order. All arrays and lists are size 26.

List<String^>^ encryptAlphabet = gcnew List<String^>();
array<bool>^ wasSwapped = {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false};
Random^ rand = gcnew Random();
int temp;
encryptAlphabet = alphabet;
for(int i =0; i < 26; i++)
{
    temp = rand->Next(26);
    if(wasSwapped[temp] ==false)
    {
	encryptAlphabet[i] = alphabet[temp];
	wasSwapped[temp] = true;
    }
    else
    {
	i -= 1;
    }
}
2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by Reverend Jim
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.