【问题标题】:C# Select random element from ListC# 从列表中选择随机元素
【发布时间】:2013-10-19 13:13:21
【问题描述】:

我正在创建一个小测验控制台应用程序。 我列了一个清单,里面有 3 个问题。 如何让程序随机选择一个问题并将其打印到控制台中?

我尝试了一些不同的代码,但由于某种原因似乎无法正常工作。 这是我尝试的最后一个代码,这是我从该站点的另一个用户那里获得的,但我得到了错误:

当前上下文中不存在名称“字符串”。

“由于Quiz.Questions.main()返回void,return关键字后面不能跟对象表达式”。

这是我尝试的最后一段代码:

class Questions
{
    public static void main()
    {
        var questions = new List<string>{
            "question1",
            "question2",
            "question3"};
        int index = Random.Next(strings.Count);
        questions.RemoveAt(index);
        return questions;

    }

}

感谢大家的回复。 我通过创建一个数组而不是一个列表来解决我的问题。 这是我现在的代码:

class Questions
{
    public static void main()
    {
        string[] questions = new string[3];
        questions[0] = "question1";
        questions[1] = "question2";
        questions[2] = "question3";
        Random rnd = new Random();
        Console.WriteLine(questions[rnd.Next(0,2)]);
    }
}

【问题讨论】:

标签: c# random


【解决方案1】:

您需要一个 System.Console.WriteLine 语句。

class Questions
{
    public static void main()
    {
        var questions = new List<string>{
            "question1",
            "question2",
            "question3"};
        int index = Random.Next(questions.Count);
        System.Console.WriteLine(questions[index]);

    }
}

【讨论】:

  • strings.Count 也应该替换为 questions.Count 和 Random.Next(...) 替换为 new Random().Next(...)。
  • 谢谢阿列克谢。已更正。
  • @AlexeyMitev: new Random().Next 有一个陷阱,即如果在短时间内重复调用它,它不会创建不同的数字。
【解决方案2】:

“当前上下文中不存在名称‘字符串’”

我猜你想要

int index = random.Next(questions.Count); // according to the lower-case random see last paragraph

而不是

int index = Random.Next(strings.Count);

由于没有名称为 strings 的变量,您还是想删除一个问题。

此外,您不能从 void 方法返回某些内容。所以创建一个返回列表:

private Random random = new Random();
List<string> GetRemoveQuestion(List<string> questions)
{
        int index = random.Next(questions.Count);
        questions.RemoveAt(index);
        return questions;
}

编辑:最后但同样重要的是,您不能使用Random.Next。那将假定Random 中有一个static Next 方法,但事实并非如此。因此,我在上面展示了如何创建和使用实例。请注意,您不应该在方法本身中创建它,因为它是用当前时间播种的。如果您非常快速地调用此方法,您将经常获得相同的“随机”值。

查看msdn at the remarks section了解更多详情。

【讨论】:

  • 很确定 Random 没有静态的 Next 方法
  • @WeylandYutani:谢谢,相应地编辑了我的答案。
【解决方案3】:

您确定要删除一个问题并返回其余问题吗? 你不应该只选择一个吗?像这样的东西:

public static void main()
{
    var random = new Random();
    var questions = new List<string>{
        "question1",
        "question2",
        "question3"};
    int index = random.Next(questions.Count);
    Console.WriteLine(questions[index]);
}

【讨论】:

    【解决方案4】:

    你有几个小错误。

    您需要对 Rand 对象的引用。您正在查看 strings 而不是 questions。您正在删除一个元素而不是选择它。

    试试这个:

    void Main()
    {
        Random rand = new Random();
        var questions = new List<string>{
            "question1",
            "question2",
            "question3"};
        int index = rand.Next(questions.Count);
        return questions[index];
        // If you want to use Linq then
        // return questions.Skip(index).Take(1);
    }
    

    【讨论】:

      【解决方案5】:

      这样的东西可能是你想要的:

      private Random rng;
      T PickRandom<T>(List<T> list)
      {
          return list[rng.NextInt(list.Count)];
      }
      

      您可以在列表中调用它以从中获取随机元素。

      【讨论】:

      • 我认为 Random.NextInt() 在 c# 中不存在(它在 Java 上存在)。如果我是正确的,代码应该是 rng.Next(list.Count) 而不是 rng.NextInt(list.Count)。
      【解决方案6】:

      试试这样的

      public static void main()
      {
          var questions = new List<string>{
              "question1",
              "question2",
              "question3"};
          Random rnd = new Random();
          int index = rnd.Next(questions.Count)
          string question  = questions[index];
          questions.RemoveAt(index); // Are you sure you neex to remove?
      
          System.Console.WriteLine(question);
      }
      

      在您使用string 而不是questions 的地方有一个错字。另外,Random 对象需要初始化。

      【讨论】:

        【解决方案7】:

        对于其他搜索者的好处:如果您想要一个耗尽列表,以确保您以随机方式使用所有项目,那么请执行以下操作:

        //use the current time to seed random so it's different every time we run it
        Random rand = new Random(DateTime.Now.ToString().GetHashCode());
        var list = new List<string>{ "item1", "item2", "item3"};
        
        //keep extracting from the list until it's depleted
        while (list.Count > 0) {
            int index = rand.Next(0, list.Count);
            Console.WriteLine("Rand Item: " + list[index]);
            list.RemoveAt(index);
        }
        

        【讨论】:

        • 注意:对rand.Next的调用应该list.Count作为第二个参数,而不是this suggested edit中建议的list.Count - 1作为@987654327的第二个参数@ 是排他的。建议的修改应该被拒绝。
        猜你喜欢
        • 2023-01-11
        • 2021-12-30
        • 2014-02-07
        • 2018-03-25
        • 2012-03-12
        • 2011-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多