Hi all. I have a dictionary of a list of strings. Each key can have more than 1 value associated with it. My problem is, when I use the following code to print out a random entry, I don’t get what I asked for:

void PullRandom() {
System.Random rand = new System.Random();
string word;
word = dbCopy.ElementAt(rand.Next(0, dbCopy.Count)).Value.ToString();
print(word);
}
EDIT: After playing around with this a little bit, I got the following output with this code (and yes, the dictionary key is in Russian):

void PullRandom() {
StringBuilder builder = new StringBuilder();
System.Random rand = new System.Random();
string word;
word = dbCopy.ElementAt(rand.Next(0, dbCopy.Count)).ToString();
builder = builder.Append(word);
print(builder);
}
This is fine, however. It looks like it wants to add the list of values following the key… I don’t need them combined like that and I don’t see where that is occurring if I am only grabbing the key.
So. What I don’t understand is:
- Why does ToString() not work in this
case?
- How can I get this to print out the
actual string instead of whatever
else it is trying to do?
- How can I print out the value
separately from the key instead of them being combined in the same
line?
You can read about lists here:
and about a dictioanries here:
PS: You can add some null checks if you are not sure something like:
if (randomListValues == null)
{
print("randomListValues is null");
}
then you will find what element is null.