22

I am creating a little quiz console application. I have made a list with 3 questions in it. How can I let the program randomly select a question and print it out int the console?

I have tried some different codes but can't seem the get it working for some reason. This is the last code I tried, which I got from another user from this site, but I get the errors:

The name 'string' does not exist in the current context.

"Since Quiz.Questions.main() returns void, a return keyword must not be followed by an object expression".

Here is the last piece of code which I tried:

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;

    }

}

Thank you all for your responses. I have fixed my problem by creating an array instead of an List. This is my code now :

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)]);
    }
}
Arsen Khachaturyan
  • 7,904
  • 4
  • 42
  • 42
Rick Velt
  • 267
  • 1
  • 2
  • 6

7 Answers7

25

Are you sure that you want to remove a question and return the rest of the questions? Should you not only select one? Somthing like this :

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]);
}
Jonas W
  • 3,200
  • 1
  • 31
  • 44
6

You need a System.Console.WriteLine statment.

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]);

    }
}
Bill Gregg
  • 7,067
  • 2
  • 22
  • 39
6

For other searchers benefit: If you want a depleting list so you ensure you use all items in a random fashion then do this:

//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);
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
JJ_Coder4Hire
  • 4,706
  • 1
  • 37
  • 25
  • Note: the call to [`rand.Next`](https://learn.microsoft.com/en-us/dotnet/api/system.random.next?view=net-5.0#System_Random_Next_System_Int32_System_Int32_) *should* have `list.Count` as the second argument rather than `list.Count - 1` as suggested in [this suggested edit](https://stackoverflow.com/review/suggested-edits/28214949) as the second argument to `Random.Next(int, int)` is exclusive. The suggested edit should be rejected. – Wai Ha Lee Feb 01 '21 at 10:44
3

Try something like this

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

There is a typo in where you are using string instead of questions. Also, Random object needs to be initalised.

Kami
  • 19,134
  • 4
  • 51
  • 63
2

Something like this could be what you want:

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

You can call it on your list to get a random element from it.

Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104
  • 1
    I think that Random.NextInt() do not exists in c# (it does on Java). If I am correct, the code should be rng.Next(list.Count) instead of rng.NextInt(list.Count). – Lotan Apr 13 '20 at 15:22
1

"The name 'string' does not exists in current context"

I assume you want

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

instead of

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

Since there is no variable with the name strings and you want to remove one question anyway.

Also, you cannot return something from a void method. So create one that returns the list:

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

Edit: last but not least, you cannot use Random.Next. That would presume that there is a static Next method in Random which is not the case. Therefore i have shown above how you create and use an instance. Note that you should not create it i the method itself since it is seeded with the curent time. If you'd call this method very fast you'd get the same "random" value often.

Have a look at msdn at the remarks section for more details.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

You have a couple of minor errors.

You need a reference to a Rand object. You are looking at strings instead of questions. You are removing an element instead of selecting it.

Try this:

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);
}
Tom Chantler
  • 14,753
  • 4
  • 48
  • 53