Pulling and printing a random entry from a dictionary of a list of strings. C#.
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?
that is my dictionary of a list of strings:
Dictionary<string, List<string>> dbCopy = new Dictionary<string, List<string>>();
int this line:
word = dbCopy.ElementAt(rand.Next(0, dbCopy.Count)).ToString();
You converts to string a whole List from dbCopy value.
Thanks, I actually just noticed that, I've been trying to update as I go along. But what about question #3?
Look into the code. I think it should solve your problem.
For prints values from the List:
use simple foreach loop
after this line: List list = dbCopy[random$$anonymous$$ey];
add this :
foreach(string s in list) { print(s); }
Thanks, I have one last problem. Printing the items out works, but I want each item added to a new List however I get the following error when I call PullRandom()
NullReferenceException: Object reference not set to an instance of an object TextChanger.PullRandom () (at Assets/Scripts/TextChanger.cs:83)
I don't see where there is an object reference needed. And for some reason, I had to set newList to null to make a compiler error go away, but then it shows up in Unity anyways. Why didn't I have to do this with randomListValues?
void PullRandom() {
List<string> randomListValues;
List<string> newList = null;
System.Random rand = new System.Random();
int randNum;
string word;
for(int i = 0; i < 5; i++) {
randNum = rand.Next(0, dbCopy.Count);
word = dbCopy.ElementAt(randNum).$$anonymous$$ey;
randomListValues = dbCopy.ElementAt(randNum).Value;
for (int y = 0; y < randomListValues.Count; y++) {
string newWord = randomListValues[y];
newList.Add(newWord);
print(newWord);
}
}
}
Answer by Masterio · Dec 15, 2015 at 09:26 PM
You can read about lists here:
http://www.dotnetperls.com/list
and about a dictioanries here:
http://www.dotnetperls.com/dictionary
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612052330im_/https://answers.unity.com/themes/thub/images/avi.jpg)