Search code examples
c#randomgeneratorstack-overflow

StackOverflow with Random Name Generator in C#


using System;


namespace npcnames

{
    class Program
{



    static string RandomVowel()
    {
        Random rand = new Random(); 
        string[] Vowels = new string[5] { "a", "e", "i", "o", "u" };
        int index = rand.Next(Vowels.Length); 
        string Vowel = Vowels[index]; 

        return Vowel;
    }

    static string RandomConsonant()
    {
        Random rand = new Random(); 
        string[] Consonants = new string[21] { "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z" };
        int index = rand.Next(Consonants.Length); 
        string Consonant = Consonants[index]; 

        return Consonant;
    }

    static string MaleName()
    {
        string malename = RandomConsonant() + RandomVowel() + RandomConsonant() + RandomVowel() + RandomConsonant();
        return MaleName();
    }

    static string FemaleName()
    {
        string femalename = RandomVowel() + RandomConsonant() + RandomVowel() + RandomConsonant() + RandomVowel();
        return FemaleName();
    }

    static void generateFemaleName(int from, int to, int step)
    {
       

        for (int a = from; a <= to; a = a + step)
        {
         
            Console.WriteLine("Female:");
            Console.WriteLine(a + FemaleName());
        }


    }

    static void generateMaleName(int from, int to, int step)
    {
        

        for (int b = from; b <= to; b = b + step)
        {

            Console.WriteLine("Male:");
            Console.WriteLine(b + MaleName());
        }


    }






    static void Main(string[] args)
    {
        generateFemaleName(1,10,1);
        Console.WriteLine();
        generateMaleName(1,10,1);

    }
}

Hi guys I'm new to coding and everything, if anyone can help me how to resolve this issue I would really appreciate it. Problem is that in my code I keep getting stack overflow and I don't know how to prevent it to execute my program normally. The purpose of the program is to generate male and female names of the random selected vowels and consonants in the list of 10 for each.


Solution

  • The issue is here:

    static string FemaleName()
    {
       string femalename = RandomVowel() + RandomConsonant() + RandomVowel() + RandomConsonant() + RandomVowel();
       return FemaleName(); //  <== bam!
    }
    

    Change:

    return FemaleName();
    

    to:

    return femalename;
    

    The short story is the method is continually calling itself. It's exactly the same for MaleName().

    Also, while we are fixing things, Random initialization should be outside the method:

    private static Random rand = new Random(); 
    
    static string RandomConsonant()
    {
       string[] Consonants = new string[21] { "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z" };
       int index = rand.Next(Consonants.Length); 
       string Consonant = Consonants[index]; 
    
       return Consonant;
    }