vendredi 13 avril 2018

same random sentence c#

i was working on random sentence generator

namespace testovani
{
    class Program
    {
        public static string[] Article = new string[] { "the", "a", "one", "some", "any" };
           public static string[] Noun = new string[] { "boy", "girl", "dog", "town", "car" };
           public static string[] Verb = new string[] { "drove", "jumped", "ran", "walked", "skipped" };
           public static string[] Preposition = new string[] { "to", "from", "over", "under", "on" };
        public static string ConstructSentence()
        {
            string word1 = getWord("Article");
            word1 = capitalize(word1);
            string word2 = getWord("Noun");
            string word3 = getWord("Verb");
            string word4 = getWord("Preposition");
            string word5 = getWord("Article");
            string word6 = getWord("Noun");

            return String.Format("{0} {1} {2} {3} {4} {5}.", word1, word2, word3, word4, word5, word6);

        }

        private static string getWord(string arrayName)
        {
            Random rnd = new Random();
            switch (arrayName)
            {
                case "Article":
                    rnd = new Random();
                    return Article[rnd.Next(0, 4)];
                case "Noun":
                    rnd = new Random();
                    return Noun[rnd.Next(0, 4)];
                case "Verb":
                    rnd = new Random();
                    return Verb[rnd.Next(0, 4)];
                case "Preposition":
                    rnd = new Random();
                    return Preposition[rnd.Next(0, 4)];
                default:
                    return "Something went wrong";
            }
        }

        static void Main()
        {
           Console.WriteLine(ConstructSentence());
           Console.WriteLine(ConstructSentence());

         Console.Read(); 


        }

    }
}

but there is a problem, if i call Console.WriteLine(ConstructSentence()); two times, i get the same result in console

how can i edit random number to get every time i call this function different words




Aucun commentaire:

Enregistrer un commentaire