2014-01-30 98 views
0

我有两个字典(由XML的电子表格导入与几个嵌套的foreach循环的数据填写) 例Excel的XML看起来是这样的: enter image description here如何从字典中挑选随机值,并将其分配到列表

Dictionary<string, string> sheet3 = new Dictionary<string, string>(); 
Dictionary<string, List <KeyValuePair<string,string>>> sheet4 = new Dictionary 
            <string, List<KeyValuePair<string,string>>>(); 

他们每个人包括:

sheet3.Add(situation, succession) 
sheet4.Add(srcSituation, new List<KeyValuePair<string,string>>()); 
sheet4[srcSituation].Add(new KeyValuePair<string, string>(dstSituation, command)); 

在Sheet4我可以看到某种 “秩序”。 Sheet3显示情况和这种情况的感觉,看起来像。在Sheet4中,我有命令,你可以看到,第一个是训练,然后进入饥饿状态,然后饥饿渐渐疲惫,之后疲倦进入觉醒状态。 dstSituation中的每一个都需要在srcSituation和dstSituation之间完成“命令”才能实现。

我想从“sheet4”中随机选择“dstSituation”并将“command”写入一个txt文件。然后查找“情况”==“dstSituation”并将“继承”字符串添加到同一个txt文件中。接下来,我会再次启动整个事情,但假设可以选择dstSituation“是在哪里”srcSituation“只是==以前选择”dstSituation“。依此类推......

这就是说,一旦我获得更多的“情境”会有更多的srcSituation-> dstSituation和ofc在Sheet4中会有更多相同的srcSituation进入不同的dstSituation。

想象一下,这是一个没有1条路线来实现某个目标的地图,但可以是随机的路线。我不知道怎么写,因为它是主要环路上的字典和列表操作,我不知道如何构建正确的语法。

+0

任何人都可以提供帮助吗? – Jan

+0

只生成随机int,然后使用Dictionary.ElementAt(int);' –

回答

0

试试这个代码段。

......Codes before 

Random rnd = new Random(); 
int randomValue = rnd.Next(0, sheet3.Count-1); 
var result = sheet3.ElementAt(randomValue); 
/* 
* Your code here 
*/ 
相关问题