Randomizer

mail@pastecode.io avatar
unknown
csharp
a month ago
1.2 kB
3
Indexable
Never
string[] names = new string[]
{
    "name1",
    "name2",
    "name3",
    "name4",
    "name5",
};

Random random = new Random();

string[] randomNames = new string[0];

Console.Write($"Введите кол-во случ. чисел (1-{names.Length}): ");
int randomInput = Convert.ToInt32(Console.ReadLine());

if (randomInput < 1 && randomInput > names.Length)
{
    Console.WriteLine("Вы указали не верное число");
    return;
}

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]);
}
Leave a Comment