需要帮助创建方法列表并从列表中随机调用方法 [英] Need help with creating list of methods and randomly call methods from list

查看:71
本文介绍了需要帮助创建方法列表并从列表中随机调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码创建方法列表和随机函数正在工作,但如果我调用 lista(); 它会反复抛出函数 - 它不会从列表中删除它们,所以我会得到: TrecePitanje() next PrvoPitanje() next PrvoPitanje() next PrvoPitanje() next TrecePitanje( )下一个 DrugoPitanje() ....等等。

对不起我的英语。









This code create List of methods and random function is working but if i call lista(); it will throw function over and over again - it will not remove them from list, so i will get: TrecePitanje() next PrvoPitanje() next PrvoPitanje() next PrvoPitanje() next TrecePitanje() next DrugoPitanje() .... and so on.
Ps sorry for mine english.




public void lista()
        {
       

            var actionList = new[]
                {
                    new Action( () => PrvoPitanje() ),
                    new Action( () => DrugoPitanje() ),
                    new Action( () => TrecePitanje() )
                }.ToList();
            
        
            label4.Text = actionList.Count().ToString(); // To Check -1 
            
            while (actionList.Count() > 0)
            {
            var r = new Random();
            var index = r.Next(actionList.Count());
            var action = actionList[index];
            actionList.RemoveAt(index);
            action();
            }




        }





< b>我尝试了什么:



如果我尝试使用



What I have tried:

If i try to count methods in list with

label4.Text = actionList.Count().ToString();



一切看起来都不错。我做错了什么?


everything looks ok. What i doing wrong?

推荐答案

当你这样做时



When you do this

new Action(()=>methodname())





你'重新执行方法。这真的是你想要的吗?



尝试这样的事情。





You're executing the methods. Is that really what you want?

Try something like this instead.

// Use a list instead of casting a var to a list (that's inefficient)
List<Action> actionList = List<Action>()
{
    // there's no need to do "new Action here"
    PrvoPitanje,
    DrugoPitanje,
    TrecePitanje
};

// instantiate the random object outside the loop
Random r = new Random();
// while we have an action in the list
while (actionList.Count() > 0)
{
    // If the list contains more than one item, randomly select the index.
    // Otherwise, just take the first and only item in the list (there's
    // no use in randomly selecting the next item when there's only one in
    // the list.
    int index = (actionList.Count > 1) ? r.Next(0,actionList.Count()-1) : 0;
    // execute the action
    actionList[index]();
    // remove it from the list
    actionList.RemoveAt(index);
}





我也会停止使用 var 除非绝对必要。仅仅因为语言具有给定的功能并不意味着它应该被使用(我不喜欢使用 var - 这是个人的事情。)



I would also stop using var unless absolutely necessary. Just because the language has a given feature doesn't mean it should be used (I dislike the use of var - it's a personal thing).


问题可能是你在循环中创建了Random实例 - 并且Random从系统时钟初始化自己,所以如果两个实例紧密地创建在一起,它们很可能会得到同样的时间,并且因此生成相同的序列。



始终将Random实例创建为私有类级实例:

The problem may be that you are creating the Random instance inside your loop - and Random initialises itself from the system clock, so if two instances are created close together, they are more than likely going to get the same time, and thus generate the same sequence.

Always create your Random instance as a private class level instance:
private Random rand = new Random();
        public void lista()
        {
            var actionList = new[]
                {
                    new Action( () => PrvoPitanje() ),
                    new Action( () => DrugoPitanje() ),
                    new Action( () => TrecePitanje() )
                }.ToList();
            label4.Text = actionList.Count().ToString(); // To Check -1 
            while (actionList.Count() > 0)
            {
                var index = rand.Next(actionList.Count());
                var action = actionList[index];
                actionList.RemoveAt(index);
                action();
            }



然后检查你的方法本身不会调用lista!


Then check that your methods don't call lista themselves!


这篇关于需要帮助创建方法列表并从列表中随机调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆