Jump to content

Assign random values for Dictionary keys

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
chipbu

chipbu

    Newbie

  • Members
  • Pip
  • 9 posts
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;

                    }


                }

            }


#2
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
yes.. you are right. The while loop is generating the problem, it is an infinite loop
while (value == alphabetList[alphabet[j]])   // this conditon is going to be always true.
                    {
                        value = ranNumber.Next(26); // generating a new value
                        alphabetList[alphabet[i]] = value;  // assigning to the dictionary 

                         // and checking for the same value.. so it is going into an infinite loop
                    }

So, I think its better for you to check the value before adding it to the dictionary... Here is the code:
Random ranNumber = new Random();
            int value = ranNumber.Next(26);
            int j = 0;
            for (int i = 0; i < alphabet.Length; i++)
            {
                while (j < alphabetList.Count)
                {
                    if (value == alphabetList[alphabet[j]])
                    {
                        value = ranNumber.Next(26);
                        j = 0;
                    }
                    else
                    {
                        j++;
                    }

                }

                alphabetList.Add(alphabet[i], value);
                value = ranNumber.Next(26);
                j = 0;
                Console.WriteLine(value + "\t" + alphabetList.Count + "\t" + alphabet[i]);
            }

Hope this helps you....

#3
chipbu

chipbu

    Newbie

  • Members
  • Pip
  • 9 posts
Sorry for not being clear enough. Actually, I want to assign to each of the key a random value in the range 0 to 25 and each key's value has to be different from the others. Your code works all right but not the way I want it to. (I actually rewrite the code that is similar to yours)

#4
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
Actually The code I gave you is correct. I guess it is what you needed....
I too checked it using this:
for (j = 0; j < alphabetList.Count; j++)
            {
                Console.WriteLine(alphabet[j].ToString() + "    " + alphabetList[alphabet[j]]);
            }
Now I can understand your problem..
since this code:
Console.WriteLine(value + "\t" + alphabetList.Count + "\t" + alphabet[i]);
is at the end of the for loop. The random value generated inside the while is not printed
while (j < alphabetList.Count)
                {
                    if (value == alphabetList[alphabet[j]])
                    {
                        value = ranNumber.Next(26);
                        j = 0;
                    }
                    else
                    {
                        j++;
                    }

                }
So only you weren't able to see the exact result.

I hope you would have understood the problem....

#5
chipbu

chipbu

    Newbie

  • Members
  • Pip
  • 9 posts
Thanks you very much. I've tested it again and it worked all right

#6
chipbu

chipbu

    Newbie

  • Members
  • Pip
  • 9 posts
btw, could you help me with this code. I was trying to fill a 5x5 char array randomly. But once more, I've got some problem with random numbers here.
I want to generate the coordinates (position) of a character then assign a value at that particular position, but the condition for the while loop is always false so I cant check whether that position has been used before.
public static readonly char[] alphabet = { 'A', 'B', 'C', 'D', 'E', 
                                               'F', 'G', 'H', 'I' , 'K',
                                               'L', 'M', 'N', 'O', 'P', 
                                               'Q', 'R', 'S', 'T', 'U',
                                               'V', 'W', 'X', 'Y', 'Z'}; 
private char[,] grid = new char[5,5];
private void Build()
        {
            Random ranNumber = new Random();
            int row, col;

            for (int i = 0; i < Digraph.alphabet.Length; i++)
            {
                row = ranNumber.Next(5);
                col = ranNumber.Next(5);
                while (grid[row, col] == null)
                {
                    row = ranNumber.Next(5);
                    col = ranNumber.Next(5);
                }
                grid[row, col] = Digraph.alphabet[i];
            }
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    Console.Write(grid[i, j] + " ");
                }
                Console.WriteLine("|");
            }
        }

NOTE: I remove J from the alphabet in this reply so that I have 25 letter (5x5).

#7
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
The while loop you are checking should not be
while (grid[row, col] == null)
It should be
while (grid[row, col] == '\0')

Hope this helps you....