12,063,645 members (57,670 online)
Rate this:
See more:
Hello! I'm making my own deck of cards. I was trying to make it "draw" two random keys and match 'em togheter and do a if- to check them.
What is the command to make it draw a random key from a dictionary?
I'm supposed to use a kortlek.Keys command, but i dont seem to get it to work
```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, int> kortlek = new Dictionary<string, int>();
for (int i = 2; i <= 10; i++)
{
}
{
}
Random dealer = new Random();

int cardone = dealer.Next(0, 53);
int cardtwo = dealer.Next(0, 53);
//HERE I'M SUPPOSED TO USE THE VALUES IN CARDONE/CARDTWO TO GRAB A CARD FROM THE DECK AND THEM MATCH 'EM.
if (cardone == cardtwo)
{
Console.WriteLine("It's a pair!");
}
else
{
Console.WriteLine("No pair :(");
}

}
}
}
```
Posted 15-Feb-12 12:44pm
Member 7896310 15-Feb-12 18:21pm

Rate this:

## Solution 2

Hello

1. generate 2 random varous numbers>=0 and < keys.Count.

How to generate many random various numbers?[^]

```int first, second;

2.

```string key1 = ((kortlek.Keys).ToArray())[first];
string key2 = ((kortlek.Keys).ToArray())[second];```
Member 7896310 15-Feb-12 19:15pm

How do I make it randomize 1000 times then check how many times there was a PAIR?
Shahin Khorshidnia 15-Feb-12 20:04pm

It's another question, but:

public int[] RandomNumbers(int min, int max)
{
return RandomNumbers(min, max, 2);
}

private Random random;
public int[] RandomNumbers(int min, int max, int derangement)
{
if (min > max)
{
throw new Exception("The first parameter must be less (or equal) than the second.");
}
int count = max - min; ;
int[] tempList = new int[count + 1];
int counter = 0;
for (int i = min; i <= max; i++)
{
tempList[counter] = i;
counter++;
}

for (int i = 0; i < derangement; i++)
for (int j = 0; j < count; j++)
{
int k = random.Next(0, count + 1);
int l = random.Next(0, count + 1);
if (k != l)
{
//Swap TempList[k] with TempList[l]
tempList[k] += tempList[l];
tempList[l] = tempList[k] - tempList[l];
tempList[k] = tempList[k] - tempList[l];
}
}
return tempList;
}

private void Check(Dictionary kortlek)
{
int count = 0;

random = new Random();

for (int i = 0; i < 1000; i++)
{
int[] numbers = RandomNumbers(0, kortlek.Keys.Count() - 1);

string key1 = ((kortlek.Keys).ToArray())[numbers[0]];
string key2 = ((kortlek.Keys).ToArray())[numbers[1]];

if (kortlek[key1] == kortlek[key2])
count++;

}

MessageBox.Show(count.ToString());
}
Rate this:

## Solution 1

If you do not insist on Dictionary, you may want to replace it with List<string,int>>

ex:

```List<keyvaluepair><string,int>> kortlek = new List<keyvaluepair><string,int>>();
...

// you can reach it like this
//kortlek[cardone].Value
</keyvaluepair>```

Hope it can help
v2
Member 7896310 15-Feb-12 18:42pm

Unfortunately the tutorial more or less insists on using the dictionarys keys.. How do I use the kortlek.keys? is it a method?

No, It is not a method.
you can use it like this: foreach (string s in kortlek.Keys) { // access to the value like this kortlek[s] }

Top Experts
Last 24hrsThis month
 CHill60 220 CPallini 215 d@nish 200 ppolymorphe 99 Chris Maunder 85
 Dave Kreskowiak 1,827 Richard MacCutchan 1,250 d@nish 980 OriginalGriff 844 CPallini 740