Hi guys,
I've got this code to assign random values for each dictionary key. Basically, I after I assign the random values, I wanna check that if the value for a particular key is the same as any previous ones then generate new random value and assign it to the key.
Example: 1st random value is 1 assign it to A, if 2nd random value is the same as the 1st one then generate a new value then assign it to B and so on.
I just got stuck in the while loop (I think) because it can work all right as long as the newly generated random number is not the same as previous ones.
Appreciate any help, thanks.
public static readonly char[] alphabet = { 'A', 'B', 'C', 'D', 'E',
'F', 'G', 'H', 'I', 'J' , 'K',
'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U',
'V', 'W', 'X', 'Y', 'Z'};
private Dictionary<char, int> alphabetList = new Dictionary<char,int>();
public Crack() // Constructor for a class named Crack
{
BuildRandomAlphaList();
}
private void BuildRandomAlphaList()
{
Random ranNumber = new Random();
int value = ranNumber.Next(26);
for (int i = 0; i < alphabet.Length; i++)
{
alphabetList.Add(alphabet[i], value);
value = ranNumber.Next(26);
Console.WriteLine(value + "\t" + alphabetList.Count + "\t" + alphabet[i]);
for (int j = 0; j < alphabetList.Count; j++)
{
while (value == alphabetList[alphabet[j]])
{
value = ranNumber.Next(26);
alphabetList[alphabet[i]] = value;
}
}
}