Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I'm generating a list of 100 random "names", now I need to follow it up with 100 more names and 100 random numbers. C#

I’m making a program that generates the "names" (random lines of text from the ASCII) that are the names of movies in this instance. I should follow them up with a "name" of a director for each (can also be generated from the ASCII), and after that the random year that is the year the "movie" was made (from 1896 to 2021).

I have two separate functions that randomize the names of the movies and directors, but I’m confused with the supposed placement of the Console.Writeline which the intelligence only allows inside their own loops. Otherwise it doesn’t seem to be able to use the values "directorname" and "moviename".

I need it to write the names in a single line, ai. (KHGTJ, KGHTJF).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Also I need a way to generate a random year from 1896 to 2021 that is printed after the names of the movie, and director, ai. (KFJU, MDDOS, 1922).

private static void GenerateRandomNames()
        {
            Random random = new Random();
            char y = (char)65;

            for (int p = 0; p < 100; p++)
            {
                string directorname = "";
                for (int m = 0; m < 5; m++)
                {
                    int b = random.Next(65, 90);
                    y = (char)b;
                    directorname += y;
                }
                Console.WriteLine(directorname);
            }

            Random rnd = new Random();
            char x = (char)65;

            for (int j = 0; j < 100; j++)
            {
                string moviename = "";
                for (int i = 0; i < 5; i++)
                {
                    int a = rnd.Next(65, 90);
                    x = (char)a;
                    moviename += x;

                }
                Console.WriteLine(moviename);
            }
            Console.WriteLine();

I need to fix the plecement of the Console.Writeline() so it can print both names in the same line, and be able to print the year after them.

I’ve tried placing the Console.Writeline() outside the loops, but of course it can’t then use the name. But this way it prints them the wrong way.

>Solution :

If you want to have minimal changes in your code, you can use the following code:

    private static void GenerateRandomNames()
    {
        //a separate thing for the names of the directors (ASCII)

        // then for the years they were made (1896-2021)

        //they should all be printed in the end ie. (KGMFK, JDBDJ, 1922)

        Random rnd = new Random();
        char x = (char)65;

       for (int j = 0; j < 100; j++)
        {
            string directors = "";
            string moviename = "";
            for (int i = 0; i < 5; i++)
            {
                int a = rnd.Next(65, 90);
                x = (char)a;
                moviename += x;

            }

            for (int i = 0; i < 5; i++)
            {
                int a = rnd.Next(65, 90);
                x = (char)a;
                directors += x;

            }
            Console.WriteLine("( "+directors +", "+ moviename + ", " +rnd.Next(1896, 2021).ToString()+" )");
        }
        Console.WriteLine();
    }

and result:

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading