Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Dictionary
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++)
            {
                kortlek.Add("klöver" + i, i);
                kortlek.Add("spader" + i, i);
                kortlek.Add("ruter" + i, i);
                kortlek.Add("hjärter" + i, +i);
            }
            {
                kortlek.Add("spader dam", 12);
                kortlek.Add("spader knekt", 11);
                kortlek.Add("spader ess", 1);
                kortlek.Add("spader kung", 13);
                kortlek.Add("hjärter ess", 1);
                kortlek.Add("hjärter dam", 12);
                kortlek.Add("hjärter knekt", 11);
                kortlek.Add("hjäter kung", 13);
                kortlek.Add("ruter ess", 1);
                kortlek.Add("ruter dam", 12);
                kortlek.Add("ruter knekt", 11);
                kortlek.Add("ruter kung", 13);
                kortlek.Add("klöver kung", 13);
                kortlek.Add("klöver ess", 1);
                kortlek.Add("klöver dam", 12);
                kortlek.Add("klöver knekt", 11);
            }
            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
Comments
Member 7896310 at 15-Feb-12 18:21pm
   
anyone, please?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hello
 
1. generate 2 random varous numbers>=0 and < keys.Count.
 
How to generate many random various numbers?[^]
 
int first, second;
//Follow Link Above...
 
2.
 
string key1 = ((kortlek.Keys).ToArray())[first];
string key2 = ((kortlek.Keys).ToArray())[second];
  Permalink  
Comments
Member 7896310 at 15-Feb-12 19:15pm
   
How do I make it randomize 1000 times then check how many times there was a PAIR?
Shahin Khorshidnia at 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: bad
good
Please Sign up or sign in to vote.

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>>(); 
...
kortlek.Add(new KeyValuePair<string,int>("spader dam", 12));
 

 
// you can reach it like this 
//kortlek[cardone].Value
</keyvaluepair></keyvaluepair>
 
Hope it can help
  Permalink  
v2
Comments
Member 7896310 at 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?
aidin Tajadod at 15-Feb-12 19:06pm
   
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] }

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 130
4 DamithSL 105
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 15 Feb 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100