得到一个不是最后一个的随机数?

时间:2017-10-21 16:47:37

标签: c# winforms

所以我试图做一个随机的小费生成器,显然,至少在所有这些生成之前都不应该显示相同的小费

private string getRandomTip()
{
    List<string> Titles = new List<string> {
    "You can copy the result by clicking over it",
    "Remember to press Ctrl + Z if you messed up",
    "Check web.com to update the app"};

    Random rnd = new Random();

    int Index = rnd.Next(0, Titles.Count);
    string randomString = Titles[Index];
    Titles.RemoveAt(Index);

    return randomString;
}

但由于某种原因,这会连续两次或多次重复提示。我认为我可以生成随机数,直到我得到一个不是最后一个,但这看起来像是一个糟糕的优化代码。

你能帮帮我吗?

编辑:好的,非常感谢你,我现在正在尝试这个

    public List<string> Titles = new List<string> {
        "You can copy the result by clicking over it",
        "This calculator was created by Glow on 21/10/17",
        "Click on this tip to show the next one"};

    private string getRandomTip()
    {
        string randomString;

        if (Titles.Count > 0) {
            Random rnd = new Random();

            int Index = rnd.Next(0, Titles.Count);
            randomString = Titles[Index];
            Titles.RemoveAt(Index);
        }
        else
        {
            randomString = "Those were all the tips!";
        }
        return randomString;
    }

它完美无缺。再次感谢:)

2 个答案:

答案 0 :(得分:0)

这里有两个问题。首先,每次调用GetRandomTip()方法时,都会重新初始化列表,以便始终能够从每个提示输出,并忽略对RemoveAt()的调用。你可以做的是只有当它有0个元素时才重新填充列表。通过这种方式,您可以确保在暂停列表之前,所有提示都会显示一次。

第二个问题是,您不应每次都重新初始化Random对象。您可能需要参考this post了解更多信息。

这是您的代码,已修改。它应该会产生更好的结果。

private class Test
{
    readonly Random Rnd = new Random();
    private List<string> _titles = new List<string>(); // Init the list with 0 elements, it will be filled-in on the first call to `GetRandomTip`

    private string GetRandomTip()
    {
        // fill the list if it's empty
        if(_titles.Count == 0) 
        {
            _titles = new List<string>
            {
                "You can copy the result by clicking over it",
                "Remember to press Ctrl + Z if you messed up",
                "Check web.com to update the app"
            };
        }

        int index = Rnd.Next(0, _titles.Count);
        string randomString = _titles[index];
        _titles.RemoveAt(index);

        return randomString;
    }
}

答案 1 :(得分:0)

您必须初始化Random对象以及方法之外的列表。否则,每次填写重新输入方法时,列表都会被填满。

一种解决方案可能是:

public class Tip
{
    private readonly IEnumerable<string> _originalTips = new List<string>
    {
        "You can copy the result by clicking over it.",
        "Remember to press Ctrl + Z if you messed up.",
        "Check web.com to update the app."
    };

    private IList<string> _availableTips;
    private Random _random;



    public Tip()
    {
        _availableTips = _originalTips.ToList();
        _random = new Random();
    }



    public string GetRandomTip()
    {
        if (!_availableTips.Any())
        {
            _availableTips = _originalTips.ToList();
        }

        int index = _random.Next(0, _availableTips.Count);

        string tip = _availableTips[index];

        _availableTips.RemoveAt(index);

        return tip;
    }
}