2017-10-21 91 views
-2

所以我试图做一个随机提示发生器,显然,不应该表现出同样的尖端至少要等到所有的人都被之前获取不是最后一个的随机数?

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; 
    } 

和它完美的作品。再次感谢:)

+1

每次方法被调用列表重新初始化。从这个临时列表中删除不会影响下次通话发生的任何事情。它会被所有的人重新填满。您需要将该列表存储在方法之外。 –

+0

建议您[阅读文档](https://msdn.microsoft.com/en-us/library/system.random(v = vs.110).aspx)。用默认的构造函数(相同的种子)创建一个新的随机数将创建相同的随机数序列。第1节第3段将使您朝正确的方向发展。 –

+0

这就对了非常感谢你,我已经更新了它,看起来像现在的作品。谢谢:) – Glow

回答

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; 
    } 
} 
+0

谢谢。我曾想过使用“这些都是提示!”但重新填写名单看起来更好。非常感谢您 – Glow

+0

我认为最好反复显示它们,以防您的用户错过了一些;-) – Wndrr

+0

如果问题解决了,请考虑通过点击复选标记来接受它。这向更广泛的社区表明,您已经找到了解决方案,并为答复者和您自己提供了一些声誉。没有义务这样做。 – Wndrr

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; 
    } 
} 
相关问题