0

Is it possible to have a function that receives a generic dictionary param and return a random key from it? Since dictionary "key" values can be any data type, I would like the dictionary param to be generic and return a random key from it no matter what data type. I wrote this so far but am getting an error.

Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(1, "1003206");
dict.Add(2, "1234567");
dict.Add(3, "5432567");

int randomKey = (int)RandomDictionaryKeyValue<Dictionary<int, string>>(dict);

private T RandomDictionaryKeyValue<T>(Dictionary<T, T> dict)
    {
    List<T> keyList = new List<T>(dict.Keys);

    Random rand = new Random();
    return keyList[rand.Next(keyList.Count)];
}

I'm getting error:

CS1503 Argument 1: cannot convert from 'System.Collections.Generic.Dictionary<int, string>' to 'System.Collections.Generic.Dictionary<System.Collections.Generic.Dictionary<int, string>, System.Collections.Generic.Dictionary<int, string>>'

I know how to get Access random item in list, but I don't know how to correctly pass dictionary to my method.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179

3 Answers3

4

The problem is that you have specified the same generic type for both the key and value of your dictionary.

private TKey RandomDictionaryKeyValue<TKey, TValue>(Dictionary<TKey, TValue> dict)
{
    //snip
}
DavidG
  • 113,891
  • 12
  • 217
  • 223
  • Obviously you can't create `Random` as shown in this post, but everyone knows that - so just be aware that the sample shows how to pass dictionary as parameter *and not* how to get random element from a list - https://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number – Alexei Levenkov Apr 16 '18 at 16:57
  • Good spot @AlexeiLevenkov, I've removed the content entirely. – DavidG Apr 16 '18 at 16:58
1

If you just want to get a random key from your dictionary, you just have to pass the key type and the valu type to your method, not the whole dictionary type:

private TKey RandomDictionaryKeyValue<TKey, TValue>(Dictionary<TKey, TValue> dict)
{
    List<TKey> keyList = new List<TKey>(dict.Keys);

    Random rand = new Random();
    return keyList[rand.Next(keyList.Count)];
}

And use it like:

Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(1, "1003206");
dict.Add(2, "1234567");
dict.Add(3, "5432567");

int randomKey = RandomDictionaryKeyValue<int, string>(dict);
na2axl
  • 1,778
  • 11
  • 15
0
Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(1, "1003206");
dict.Add(2, "1234567");
dict.Add(3, "5432567");

int randomKey = (int)RandomDictionaryKeyValue<int, string>(dict);

Figured out the answer before I came back here to check.

private object RandomDictionaryKeyValue<T1, T2>(object dict)
{
    var dict2 = (Dictionary<T1, T2>)dict;
    List<T1> keyList = new List<T1>(dict2.Keys);                                                                                                                        

    Random rand = new Random();
    return keyList[rand.Next(keyList.Count)];
}