Создать случайный массив в C++

Чтобы сгенерировать случайный массив в C++, вы можете использовать несколько методов. Вот несколько примеров:

Метод 1: использование библиотеки

#include <iostream>
#include <random>
#include <ctime>
int main() {
    std::mt19937 rng(std::time(nullptr));  // Initialize random number generator
    std::uniform_int_distribution<int> dist(1, 100);  // Define the range of random numbers

    const int arraySize = 10;
    int array[arraySize];

    for (int i = 0; i < arraySize; i++) {
        array[i] = dist(rng);  // Generate random number and assign it to array element
        std::cout << array[i] << " ";  // Print the generated number
    }

    return 0;
}

Метод 2: использование функции rand()

#include <iostream>
#include <cstdlib>
#include <ctime>
int main() {
    std::srand(std::time(nullptr));  // Seed the random number generator with current time

    const int arraySize = 10;
    int array[arraySize];

    for (int i = 0; i < arraySize; i++) {
        array[i] = std::rand() % 100 + 1;  // Generate random number and assign it to array element
        std::cout << array[i] << " ";  // Print the generated number
    }

    return 0;
}

Метод 3. Использование функции random_shuffle()

#include <iostream>
#include <algorithm>
#include <ctime>
int main() {
    std::srand(std::time(nullptr));  // Seed the random number generator with current time

    const int arraySize = 10;
    int array[arraySize];

    for (int i = 0; i < arraySize; i++) {
        array[i] = i + 1;  // Fill the array with values from 1 to 10
    }

    std::random_shuffle(std::begin(array), std::end(array));  // Shuffle the array

    for (int i = 0; i < arraySize; i++) {
        std::cout << array[i] << " ";  // Print the generated number
    }

    return 0;
}

Это всего лишь несколько примеров того, как можно сгенерировать случайный массив в C++. Не стесняйтесь выбирать метод, который лучше всего соответствует вашим потребностям.