Untitled

 avatar
unknown
csharp
a year ago
1.1 kB
7
Indexable
Random random = new Random();

string[] names = new string[]
{
    "example1",
    "example2",
    "example3",
    "example4",
    "example5",
    "example6",
    "example7",
    "example8",
    "example9",
    "example10"
};


string[] randomNames = new string[0];

Console.Write("Введите количество случайных чисел: ");
int randomInput = Convert.ToInt32(Console.ReadLine());

for (int i = 0; i < randomInput; i++)
{
    int randomIndex;
    while (true)
    {
        randomIndex = random.Next(names.Length);

        bool isRandomName = true;

        foreach (string name in randomNames)
        {
            if (name == names[randomIndex])
            {
                isRandomName = false;
                break;
            }
        }
        if (isRandomName)
        {
            break;
        }
    }
    string[] tempArray = new string[randomNames.Length + 1];
    for (int j = 0; j < randomNames.Length; j++)
    {
        tempArray[j] = randomNames[j];
    }
    tempArray[^1] = names[randomIndex];
    Console.WriteLine(names[randomIndex]);
}
Editor is loading...
Leave a Comment