-3

I'm quite new to C# and I made this method to create random words and it takes in a minimum length and maximum as a parameter. But when I put it in a for loop 20 times I can see that every few in a row are identical. How do I fix this?

static string makename(int min, int max)
        {
            Random rnd = new Random();
            char[] vowels = { 'a', 'e', 'i', 'o', 'u' };
            char[] consonants = { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z' };
            int namelength = rnd.Next(min, max);
            char twoago = '2';
            char oneago = '1';
            string name = "";
            for (int i = 0; i < namelength; i++)
            {
                if (oneago != twoago)
                {
                    twoago = oneago;
                    if (rnd.Next(0, 2) == 1)
                    {
                        name = name + vowels[rnd.Next(0, vowels.Count())];
                        oneago = 'v';
                    }
                    else
                    {
                        name = name + consonants[rnd.Next(0, consonants.Count())];
                        oneago = 'c';
                    }
                }
                else if (oneago == 'c')
                {
                    name = name + vowels[rnd.Next(0, vowels.Count())];
                    oneago = 'v';
                }
                else
                {
                    name = name + consonants[rnd.Next(0, consonants.Count())];
                    oneago = 'c';
                }
                if (i == 0)
                {
                    name = name.ToUpper();
                }
            }
            return name;
        }

Here is an example of the output (parameters are 4,10) (there was one output before this I cut off):console

  • 5
    `Random rnd = new Random();` This should only be declared once outside of that method. – LarsTech Oct 01 '20 at 18:51
  • You should *always* consult the [documentation](https://learn.microsoft.com/en-us/dotnet/api/system.random?view=netframework-4.8) first thing when you encounter unexpected results - this issue is well explained there. – Ňɏssa Pøngjǣrdenlarp Oct 01 '20 at 18:57
  • @LarsTech how do I solve the issue of it saying it doesn't exist in the current context? – George Bland Oct 01 '20 at 18:58
  • Can't see your changed code from here. Refer to the duplicate link I provided. If you simply moved that one line out of your method, it needs to be declared static, too. – LarsTech Oct 01 '20 at 18:59
  • @GeorgeBland, `how do I solve the issue of it saying it doesn't exist in the current context?` -- you will need to make it globally scoped. – Casey Crookston Oct 01 '20 at 19:00

1 Answers1

0

When you instantiate the Random (with new Random();) random is being reset to the system clock. Your method is getting called quickly enough that the call is sometimes getting the same system clock time. Random on a computer is never random. It is based on a fairly random mathematical function that behaves fairly randomly as long as it is seeded (started) with a different number each time.

Note, you only need to seed random once for an application run in order for it to behave randomly. It is when you seed it multiple times that you get into trouble.

amalgamate
  • 2,200
  • 5
  • 22
  • 44