简体   繁体   English

我正在生成一个包含 100 个随机“名称”的列表,现在我需要再添加 100 个名称和 100 个随机数。 C#

[英]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.我正在制作一个生成“名称”(来自 ASCII 的随机文本行)的程序,这些名称是本例中电影的名称。 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).我应该跟进他们每个导演的“名字”(也可以从 ASCII 生成),然后是随机年份,即制作“电影”的年份(从 1896 年到 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.我有两个独立的函数来随机化电影和导演的名字,但我对Console.Writeline的假定位置感到困惑,智能只允许在它们自己的循环中。 Otherwise it doesn't seem to be able to use the values "directorname" and "moviename".否则它似乎无法使用值“directorname”和“moviename”。

I need it to write the names in a single line, ai.我需要它把名字写在一行中,ai。 (KHGTJ, KGHTJF). (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.此外,我需要一种方法来生成从 1896 年到 2021 年的随机年份,该年份打印在电影名称和导演名称之后,ai。 (KFJU, MDDOS, 1922). (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.我需要修复Console.Writeline()的执行,以便它可以在同一行中打印两个名称,并能够在它们之后打印年份。

I've tried placing the Console.Writeline() outside the loops, but of course it can't then use the name.我试过将Console.Writeline()放在循环之外,但当然它不能再使用该名称。 But this way it prints them the wrong way.但是这样它以错误的方式打印它们。

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:结果:

在此处输入图像描述

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.我将随机的 object 移动到 class(使其成为 class 字段),以便它可以在不同的地方重复使用。

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.请注意, Next(Int32, Int32)方法的第二个参数是独占上限。 Therefore I added 1.因此我加了1。

output: 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

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,由于您只需要其他 5 个字母的单词和 1896 - 2021 之间的 4 位数字,

Just get another variable 'b' and do the same as you did for 'a', like:只需获取另一个变量 'b' 并像对 'a' 一样执行相同的操作,例如:

int b = rnd.Next(65,90); int b = rnd.Next(65,90); y = char(b); y = 字符 (b); director name += y;导演姓名+= y;

and to get the year value, you can use this:并获得年份值,你可以使用这个:

year = rnd.Next(1896,2021)年份 = 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(); 
    }
}

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. :)对我来说,它们似乎都是很好的答案,但自从我费心编写这段代码以来,给你 go。:)

I focused on using the same Random in different ways, because I think that's what you were asking about.我专注于以不同的方式使用相同的Random ,因为我认为这就是你所问的。

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM