create random vectors c++
To create random vectors in C++, you can follow these steps:
- Include necessary libraries:
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
- Set the seed for the random number generator:
srand(static_cast<unsigned int>(time(0)));
- Define the size of the vector:
int size = 10; // for example
- Create the vector and populate it with random values:
std::vector<int> randomVector;
for (int i = 0; i < size; ++i) {
randomVector.push_back(rand() % 100); // for example, generating random numbers between 0 and 99
}
- Display the random vector:
for (int i = 0; i < size; ++i) {
std::cout << randomVector[i] << " ";
}
These steps will allow you to create a random vector in C++ and display its contents.