1

I built this little ASCII face generator (sort of) and I want it to randomly assemble a face based on the the parts available but it currently seems to only select the parts based on whatever integer it initially draws. So if it calls 2 initially it just kind of calls the second option for all the different parts.

ex.

    {
        bool faceLoop = true;
        while (faceLoop == true)
        {
            int setHair;

            Random randHair = new Random();
            setHair = randHair.Next(1, 4);

            if (setHair == 1)
            {
                Console.WriteLine("       {}    {}    {}    {}");
                Console.WriteLine("      {  }  {  }  {  }  {  }");
                Console.WriteLine("     {    }{    }{    }{    }");
                Console.WriteLine("    {                        }");
            }
            if (setHair == 2)
            {
                Console.WriteLine("    {{{{{{{{{{{{{}}}}}}}}}}}}}");
                Console.WriteLine("    {{{{{{{{{{{{{{}}}}}}}}}}}}");
                Console.WriteLine("    {{{{{{{{{{{{{}}}}}}}}}}}}}");
                Console.WriteLine("    {{{{{{{{{{{{{{}}}}}}}}}}}}");
            }
            if (setHair == 3)
            {
                Console.WriteLine("    ,,,,,,,,,,,,,,,,,,,,,,,,,,");
            }

            Console.WriteLine("  _ }                        { _");
            Console.WriteLine(" / |  = = =            = = =  | \\");

            int setEyes;

            Random randEyes = new Random();
            setEyes = randEyes.Next(1, 4);

            if (setEyes == 1)
            {
                Console.WriteLine(" \\ |  (_#_)            (_#_)  | /");
            }
            if (setEyes == 2)
            {
                Console.WriteLine(" \\ |   xXx              xXx   | /");
            }
            if (setEyes == 3)
            {
                Console.WriteLine(" \\ |   {0}              {0}   | /");

            }

            int setNose;

            Random randNose = new Random();
            setNose = randNose.Next(1, 4);

            if (setNose == 1)
            {
                Console.WriteLine("  \\|          (,  ,)          |/");
            }
            if (setNose == 2)
            {
                Console.WriteLine(" \\ |            ||            | /");
                Console.WriteLine("  \\|           <..>           |/");
            }
            if (setNose == 3)
            {
                Console.WriteLine("  \\|             >            |/");
            }

            int setMoustache;

            Random randStash = new Random();
            setMoustache = randStash.Next(1, 4);

            if (setMoustache == 1)
            {
                Console.WriteLine("   {                          }");
                Console.WriteLine("   {         ////\\\\\\\\         }");
            }
            if (setMoustache == 2)
            {
                Console.WriteLine("   {                          }");
                Console.WriteLine("   {        .... ....         }");
            }
            if (setMoustache == 3)
            {
                Console.WriteLine("   {                          }");
                Console.WriteLine("   {                          }");
            }

            int setMouth;

            Random randMouth = new Random();
            setMouth = randMouth.Next(1, 4);

            if (setMouth == 1)
            {
                Console.WriteLine("   {0                        0}");
                Console.WriteLine("    {     X==^=========X     }");
            }
            if (setMouth == 2)
            {
                Console.WriteLine("   {{                        }}");
                Console.WriteLine("    {    \\______________/    }");
            }
            if (setMouth == 3)
            {
                Console.WriteLine("   {%                        %}");
                Console.WriteLine("    {%    <------------>    %}");
            }

            int setChin;

            Random randChin = new Random();
            setChin = randChin.Next(1, 4);

            if (setChin == 1)
            {
                Console.WriteLine("     {          ^^          }");
                Console.WriteLine("      %####################%");
            }
            if (setChin == 2)
            {
                Console.WriteLine("     {                      }");
                Console.WriteLine("      {_____|__|__|__|_____}");
            }
            if (setChin == 3)
            {
                Console.WriteLine("     {                      }");
                Console.WriteLine("      \\__________%__________/");
            }

            Console.ReadLine();
        }
    }
Ash G
  • 29
  • 6
  • 1
    The code is creating a new `Random` with each section. This will most likely lead to all the random numbers being the same number. Use ONE (1) `Random` for all the different sections. – JohnG Apr 02 '21 at 01:45

1 Answers1

1

I just ran the code below, which is all of your code above, but instead of printing that part of the face it just prints the random number generated. From what I saw, the number are being generated randomly.

 bool faceLoop = true;
            while (faceLoop == true)
            {
                int setHair;

                Random randHair = new Random();
                setHair = randHair.Next(1, 4);
                Console.WriteLine("Set hair: " + setHair);

                int setEyes;

                Random randEyes = new Random();
                setEyes = randEyes.Next(1, 4);
                Console.WriteLine("Set eyes: " + setEyes);
                int setNose;

                Random randNose = new Random();
                setNose = randNose.Next(1, 4);
                Console.WriteLine("Set nose: " + setNose);

                int setMoustache;

                Random randStash = new Random();
                setMoustache = randStash.Next(1, 4);
                Console.WriteLine("Set moustache: " + setMoustache);
                int setMouth;

                Random randMouth = new Random();
                setMouth = randMouth.Next(1, 4);
                Console.WriteLine("Set mouth: " + setMouth);
                int setChin;

                Random randChin = new Random();
                setChin = randChin.Next(1, 4);
                Console.WriteLine("Set chin: " + setChin);

                Console.ReadLine();

The output of this code is:

Set hair: 1

Set eyes: 2

Set nose: 3

Set moustache: 3

Set mouth: 1

Set chin: 3

Ntjs95
  • 128
  • 1
  • 8
  • I hate to disagree, however, using your posted code, I am unable reproduce the output you show. In almost all cases, in my tests, the number for each section was the same. As commented, this is due to the `Random` being “newly created” for each section. `Random` uses a `DateTime` as an initial seed, therefore, when newly created random numbers are created in quick succession (as is here), then there is a very high chance that the numbers will be the same. In addition, it makes no sense to create “separate” `Random` variables for each section, one (1) will work for all sections. – JohnG Apr 02 '21 at 02:26
  • 1
    That's very interesting! Whether the code I posted above works or not, you are correct that a single instance of Random is the correct way to implement the solution. – Ntjs95 Apr 02 '21 at 03:13