Member Avatar for chipbu

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;
                    }

                }
            }

Change your constructor
private Dictionary<char, int> alphabetList = new Dictionary<char,int>();
into
private Dictionary<int, char> alphabetList = new Dictionary<int,char>();
Because a Dictionary constructor first has a Key type and then a value type.
The Add method will throw an exception if you try to add a key that already exists, you can catch it like this:

try
            {
                alphabetList.Add(value, alphabet[i]);
            }
            catch (ArgumentException)
            {
                // get new random value or something
            }

Also: int value = ranNumber.Next(26); will give you values from 0 to 25. If that is OK with you, that's OK with me. Just wanted to mention it.

Change your constructor
private Dictionary<char, int> alphabetList = new Dictionary<char,int>();
into
private Dictionary<int, char> alphabetList = new Dictionary<int,char>();
Because a Dictionary constructor first has a Key type and then a value type.

I think the OP wants it like he has it because he wants to keep counts of letters like how many times does 'A' show up in a document... (I know this from another thread, I'm not psychic hehe)

Well jonsca, if you can correct me, please do :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.