-4

For school I need to make a project in C# that will ask 20 questions at random.

I have a textfile that contains the questions with the answers.

With some code we put the questions in a array. Now we can generate an number from 0 to 19 (20 questions). But each question need to appear 1 time and 1 time only.

We can't do the first at random and the next in order. Every question needs to be at random.

My code (in Dutch):

public int randomVraagMaker()
    {

        int num;
        Random randNum = new Random();
        num = randNum.Next(0, 19);
        for (int i = 0; i < lijst.Count; i++) 
        {
            if (num == lijst[i])
            {
                num = randNum.Next(0, 19);
            }
            else
                lijst.Add(num);

        }
        vraag = num * 7;
        return vraag;
Michael0x2a
  • 58,192
  • 30
  • 175
  • 224
user3574537
  • 1
  • 1
  • 4
  • 1
    If you have concrete questions, this is the place to ask them. We do, however, not do your homework for you. – kat0r Apr 25 '14 at 21:14
  • So much that I'm very confused right now :/ – user3574537 Apr 25 '14 at 21:15
  • If that was my homework i would be happy . But its a lot bigger than this. – user3574537 Apr 25 '14 at 21:16
  • 3
    Here's a tip: Get the questions in a list and then randomize the order of the list. – Brian Rasmussen Apr 25 '14 at 21:16
  • 1
    Alternatively, remove the random question from the list when you select it. Make sure your next random number only goes to the length of the list though. – DoubleDouble Apr 25 '14 at 21:18
  • 1
    possible duplicate of [Randomize a List in C#](http://stackoverflow.com/questions/273313/randomize-a-listt-in-c-sharp) – Silvermind Apr 25 '14 at 21:19
  • hey double, The fact is we need to generate all the time a number between 0 and 19. – user3574537 Apr 25 '14 at 21:19
  • hey, silvermind. I need to put the genareted number back in a int – user3574537 Apr 25 '14 at 21:21
  • 1
    Step back from what you think you need, and look at what you really want to do. You want to get the set of 20 questions in a random order with no duplicates. That's shuffling. The answer linked by @Silvermind tells you how to do shuffling in C#. – pjs Apr 25 '14 at 22:04

4 Answers4

2

You can build a list of all of your questions, and then randomly remove the question you want to ask:

// Using a List<string> questions or List<int> of indices

T RemoveSingleQuestion<T>(List<T> questions, Random random)
{
     // TODO: Add validation (ie: questions is not null, has at least 1 element, etc)
     int index = random.Next(questions.Count);
     T question = questions[index];
     questions.RemoveAt(index);
     return question;
}

This way, you can build the list (of any number of questions), and just remove the ones you ask as you go.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
1

I will not give you the answer since it is homework, however, what you'll want to do is create a list of integers:

List<int> _usedNumbers = new List<int>();

Then each random number you generate, add it to the list. Finally, after selecting your random number, check to see if its in your list:

if (_usedNumbers.Contains(myRandomNumber))
{
    // Number is not unique. Try generating another random number
} else {
    // Number IS unique!
}

I'm giving you an idea of how to do it. In reality, you'll probably want to use a while loop to keep getting a random number until you get one that isn't in the list.

Icemanind
  • 47,519
  • 50
  • 171
  • 296
  • Hey , That's what i basicly have now. But for some reason this won't work – user3574537 Apr 25 '14 at 21:23
  • if (_usedNumbers.Contains(myRandomNumber)) { myRandomNumber = randNum.Next(0, 19); } else { return myRandomNumber _usedNubers.add(myRandomNumber) } I know that this normally needs to work but it doesn't – user3574537 Apr 25 '14 at 21:28
  • You are choosing a new random number if its in the list, BUT, what if the new random number you chose is in the list too? Are you checking again? and Again? It needs to be in a `while` loop. Something like `while (_usedNumbers.Contains(myRandomNumber)) { myRandomNumber = randNum.Next(0,19); }` – Icemanind Apr 25 '14 at 21:29
  • Make sure once all the questions have been asked, you stop. Otherwise you'll create an endless loop. – Icemanind Apr 25 '14 at 21:32
  • Yeah That's taken care off :D with a basic counter – user3574537 Apr 25 '14 at 21:35
0

I'm not going to do your homework for you but maybe try thinking about how you'd solve this issue with a piece of paper and a dice that gave you numbers from 1-20. You might do this

  1. Roll the dice
  2. If the number hasn't been used ask the question and cross it off the list
  3. If the number has been used roll again
David Hayes
  • 7,402
  • 14
  • 50
  • 62
0

Instead of getting a random number and then getting the question for that number, here's what I would do...

Put all of your questions in an array, and then randomize (or shuffle) the array. That should be easy to do. Once you have a shuffled array, which might look like:

18, 4, 6, 19, 1, [...] 

Whenever you need an answer, take the one in the front (or at the next index). Every question is guaranteed to be in there once and only once at the time you finish the array.

Kyle W
  • 3,702
  • 20
  • 32