Home > Back-end >  I'm generating a list of 100 random "names", now I need to follow it up with 100 more
I'm generating a list of 100 random "names", now I need to follow it up with 100 more

Time:12-11

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).

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.

CodePudding user response:

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

CodePudding user response:

Not sure if it is good to answer this type of question, but answering it anyway.

Since you only want other 5-letter words and 4-digit numbers ranging from 1896 - 2021,

Just get another variable 'b' and do the same as you did for 'a', like :

int b = rnd.Next(65,90) ; y = char(b) ; director name = y ;

and to get the year value, you can use this :

year = rnd.Next(1896,2021)

So, by combining all of the above, you have the code like this :

internal class Program
    {
        private static void GenerateRandomNames()
        {
   
        Random rnd = new Random();
        char x = (char)65;
        char y =  (char) 65 ;

        for (int j = 0; j < 100; j  )
        {
            string moviename = "";
            string directorName = "";
            int year = rnd.Next(1896,2021);
            for (int i = 0; i < 5; i  )
            {
                int a = rnd.Next(65, 90);
                int b = rnd.Next(65, 90);
                x = (char)a;
                moviename  = x;
                y = (char)a;
                directorName  = x;             

            }

            Console.WriteLine(moviename);
            Console.WriteLine(directorName);
            Console.WriteLine(year);
        }
        Console.WriteLine();
    }
    static void Main(string[] args)
    {
        GenerateRandomNames(); 
    }
}

CodePudding user response:

The task becomes easier if you extract the creation of a random name to a new method. This allows you to call it twice easily. I moved the random object to the class (making it a class field), so that it can be reused in different places.

internal class Program
{
    private static readonly Random _rnd = new Random();

    private static string CreateRandomName(int minLength, int maxLength)
    {
        string name = "";
        for (int i = 0; i < _rnd.Next(minLength, maxLength   1); i  )
        {
            char c = (char)_rnd.Next((int)'A', (int)'Z'   1);
            name  = c;
        }
        return name;
    }

    private static void WriteRandomNames()
    {
        for (int i = 0; i < 100; i  )
        {
            string movie = CreateRandomName(4, 40);
            string director = CreateRandomName(3, 30);
            int year = _rnd.Next(1896, 2022);
            Console.WriteLine($"{movie}, {director}, {year}");
        }
        Console.WriteLine();
    }

    static void Main(string[] args)
    {
        WriteRandomNames();
    }
}

Note that the second parameter of the Next(Int32, Int32) method is the exclusive upper bound. Therefore I added 1.

output:

HATRHKYAHQTGS, NCPQ, 1999
QVJAYOTTISN, LJTGJDMB, 2018
JEXJDICLRMZFRV, GJPZHFBHOTR, 1932
SKFINIGVYUIIVBD, DIZSKOS, 1958
LWWGSEIZT, AMDW, 1950
OAVZVQVFPPBY, SPEZZE, 2008
YLNTZZIXOCNENGYUL, URNJMK, 1962
ONIN, WUITIL, 1987
RJUXGORWDVQRILDWWKSDWF, MOEYPZQPV, 1946
YUQSSOPZTCTRM, UEPPXIVGERG, 1994
KILWEYC, QJZOTLKFMVPHUE, 1915

CodePudding user response:

Wow, in the time it took me to write an answer, three or more others appeared. They all seem like pretty good answers to me, but since I went to the trouble of writing this code, here you go. :)

I focused on using the same Random in different ways, because I think that's what you were asking about.

using System;
using System.Collections.Generic;
using System.Linq;

Random rnd = new Random(1950);

GenerateRandomNames();

void GenerateRandomNames()
{
    for (int j = 0; j < 100; j  )
    {
        // here's one way to get a random string
        string name = Guid.NewGuid().ToString().Substring(0, 5); 

        string description = new string(GetRandomCharacters(rnd.Next(5,16)).ToArray());
        
        string cleaner = new string(GetCleanerCharacters(rnd.Next(5, 16)).ToArray());
        
        string preferred = new string(GetPreferredRandomCharacters(rnd.Next(5, 16)).ToArray());

        int year = rnd.Next(1896, DateTime.Now.Year   1);

        Console.WriteLine($"{year}\t{preferred}");
        Console.WriteLine($"{year}\t{cleaner}");
        Console.WriteLine($"{year}\t{name}\t{description}");
        Console.WriteLine();
    }
    Console.WriteLine();

}

// Not readable
IEnumerable<char> GetRandomCharacters(int length = 5)
{
    for (int i = 0; i < length; i  )
    {
        yield return Convert.ToChar(rnd.Next(0, 255)); 
    }
}

// gives you lots of spaces
IEnumerable<char> GetCleanerCharacters(int length = 5)
{
    for (int i = 0; i < length; i  )
    {
        char c = Convert.ToChar(rnd.Next(0, 255));
        if (char.IsLetter(c))
        {
            yield return c;
        }
        else
        {
            yield return ' ';
        }
    }
}

// Most readable (in my opinion), but still nonsense.
IEnumerable<char> GetPreferredRandomCharacters(int length = 5)
{
    for (int i = 0; i < length; i  )
    {
        bool randomSpace = rnd.Next(0, 6) == 3;

        if (i > 0 && randomSpace) // prevent it from starting with a space
        {
            yield return ' ';
            continue;
        }

        var c = Convert.ToChar(rnd.Next(65, 91)); // uppercase letters
        if (rnd.Next(0, 2) == 1)
        {
            c = char.ToLower(c);
        }
        yield return c;
    }
}
  • Related