0

So my general question is: is there a way to refer to an old random instance after declaring a new one?

First, the following is the code that I'm currently using.

This is the list I created:

        Random p1rnd = new Random();
        List<string> p1list = new List<string>();
        p1list.Add("A");
        p1list.Add("B");
        p1list.Add("C");
        p1list.Add("D");
        p1list.Add("E");
        p1list.Add("F");
        p1list.Add("G");
        p1list.Add("H");
        p1list.Add("I");
        p1list.Add("J");
        int p1r = p1rnd.Next(p1list.Count);

This is the block of code where I used the list, and is what I'm currently having an issue with:

Console.WriteLine("\nWhat would you like to say?");
                    Console.Write("A. ");
                    p1r = p1rnd.Next(p1list.Count); //CHOICE A
                    Console.Write(p1list[p1r]);
                    Console.Write("B. ");
                    p1r = p1rnd.Next(p1list.Count); //CHOICE B
                    Console.Write(p1list[p1r]);
                    Console.Write("C. ");
                    p1r = p1rnd.Next(p1list.Count); //CHOICE C
                    Console.Write(p1list[p1r]);
                    p1choiceA = Console.ReadLine();

Basically, I'm planning to use if-else statements to remove the choices. However, I don't know how to phrase my condition.

        if ( ) // SHOULD BE CHOICE A
        {
        p1list.Remove( )
        }
        else if ( ) // SHOULD BE CHOICE B
        {
        p1list.Remove( )
        }
        else if ( ) // SHOULD BE CHOICE C
        {
        p1list.Remove( )
        }

Typically, I would use p1list[p1r]. But, given that I created new random instances for each, I don't know what to put anymore. How would I refer to the instance used in CHOICE A and B? Thanks.

  • Explain what you are ultimately trying to do please. – Sweeper Jan 06 '18 at 08:22
  • @Sweeper Oops, sorry it's not clear. I'm going to use if-else statements to remove the choices. For example, if they choose CHOICE A, I want to remove that choice from the list. But if I use the code 'p1list[p1r]', it refers to the choice that came up in CHOICE C instead. Because of that, I'm wondering if there's a way to refer back to CHOICE A. (I'll put this up on the post too) –  Jan 06 '18 at 08:34
  • Which list are you talking about? Update the code part as well – Sir Rufo Jan 06 '18 at 08:37
  • @SirRufo Updated that as well. Please tell me if anything else is confusing. –  Jan 06 '18 at 08:44
  • On the next edit we need some navigation instruction for your question **or** you reorganize it for better readability. – Sir Rufo Jan 06 '18 at 08:46
  • I'll reorganize it, then. Sorry that it wasn't to your liking. –  Jan 06 '18 at 08:50
  • if you want to refer to your previous instances of the Random class at a later point, you'd have to keep them in memory, in separate variables, organised in such a way that you can relate them to the integers they generated. I think that's what you're trying to do, right? – ADyson Jan 06 '18 at 09:03
  • You can use another list to keep the items for the three choices. – Sir Rufo Jan 06 '18 at 09:10
  • @ADyson Yes. If you could expound on how to do that further, it'd really be helpful. Thank you. –  Jan 07 '18 at 11:17
  • @SirRufo Noted, will try. –  Jan 07 '18 at 11:17
  • @Sarah you've got a `List` of `string`s, you would just need a corresponding `List` of `Random`s as well. – ADyson Jan 07 '18 at 21:21

1 Answers1

0

If I understand your requirements correctly, you wish to show three random choices. The user gets to pick one, which gets removed from the list. Then he gets to see three more choices: the two remaining from the original list, plus an additional random one. Is that correct?

My suggestion:

First, sort your list into a random order. This makes everything else way easier.

//using System.Linq;
var sortedList = p1list.OrderBy( s => p1rnd.NextDouble() ).ToList();

Now that the list is sorted, it is very simple to select three questions. Just pick the first three!! And if you ever need to know which ones you picked... they were the first three. This satisfies the requirements of both randomness and recall.

To compose a list for display to the user, Zip() the list with an array containing the choice identifiers (the letters A, B, and C).

    var choiceLetters = new [] { "A", "B", "C" };

    var choiceStrings = choiceLetters.Zip
    (
        sortedList, 
        (a,b) => string.Format("{0}. {1}", a, b)
    );
    Console.WriteLine("What would you like to say?");
    foreach (var s in choiceStrings) 
    {
        Console.WriteLine(s);
    }

To remove one, just remove it from the sortedList.

    var choiceLetter = Console.ReadLine();
    var choiceIndex = Array.IndexOf(choiceLetters, choiceLetter.ToUpper());
    if (choiceIndex == -1) break;  //User didn't pick A, B, or C

    sortedList.RemoveAt(choiceIndex);

Complete solution:

public static void Main()
{
    Random p1rnd = new Random();
    List<string> p1list = new List<string>
    {
        "These sentences make up the random list of choices.",
        "In your example you used single letters.",
        "That was kind of confusing.",
        "The user always sees A, B, and C.",
        "But not sentences 0, 1, and 2.",
        "I'll have a beer please."
    };

    var sortedList = p1list.OrderBy( s => p1rnd.NextDouble() ).ToList();

    var choiceLetters = new [] { "A", "B", "C" };

    while (true)
    {
        var choiceStrings = choiceLetters.Zip
        (
            sortedList, 
            (a,b) => string.Format("{0}. {1}", a, b)
        );

        Console.WriteLine("What would you like to say?");
        foreach (var s in choiceStrings) 
        {
            Console.WriteLine(s);
        }

        var choiceLetter = Console.ReadLine();
        var choiceIndex = Array.IndexOf(choiceLetters, choiceLetter.ToUpper());
        if (choiceIndex == -1) break;  //User didn't pick A, B, or C

        sortedList.RemoveAt(choiceIndex);
    }
}
John Wu
  • 50,556
  • 8
  • 44
  • 80
  • Thanks, this is really helpful! But I'd like to ask, is it possible to add different Console.ReadLine(); for what you choose? Like, I want to add, "Your response was [response different with each choice]." Is that possible? –  Jan 07 '18 at 09:41
  • 1
    @Sarah Please note this is a Q&A-Site (Question&Answer). Anyone can ask a question and anyone can answer **that** question. If you have another question, ask that as a **new** question. BTW Asking for „Is .. possible?“ will just produce answers like „Yes, of course.“ or „No“. Do you really want to know if it is possible or how to do? If how, then ask what you really want – Sir Rufo Jan 07 '18 at 11:33
  • Noted, was just asking for clarifications. Will be sure to follow the rules next time around. –  Jan 07 '18 at 14:28
  • The user's choice is contained in `sortedList[choiceIndex]` so you can always display it with `Console.Writeline(sortedList[choiceIndex])`'. – John Wu Jan 07 '18 at 19:46