How to generate a vector with random values in C++?
Last Updated :
13 Dec, 2024
Generating a vector with random values means creating a vector of n elements and initialize each element some random values. In this article, we will learn different methods to initialize a vector with random values in C++.
The recommended method to initialize a vector with random values is by using a random number generator from C++'s <random> library. Let's take a look at an example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v(10);
// Create a random number generator between 0 to 100
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dis(0, 100);
// Initialize vector elements with random values
for (int i = 0; i < 10; ++i)
v[i] = dis(gen);
for (auto i : v)
cout << i << " ";
return 0;
}
Output61 63 100 12 21 24 52 61 51 38
Explanation: The <random> library allows you to generate numbers in a specific range using distribution objects like uniform_int_distribution.
C++ also provides some other methods to generate a vector with random values. Some of them are as follows:
Using rand() Function
The rand() function generates pseudo-random numbers which can be used to populate a vector by calling rand() for each element and storing the results.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v(10);
// Generate 10 random numbers between 0 and 100
for (int i = 0; i < 10; ++i)
v[i] = rand() % 100;
for (auto i : v)
cout << i << " ";
return 0;
}
Output83 86 77 15 93 35 86 92 49 21
Explanation: The rand() function generates pseudo-random integers. The modulo operator (%) is used to limit the range of random values.
Using generate() with Random Function
The generate() function from <algorithm> allows you to populate a vector with given values. You can use the random number generator to provide the random values for generate() function.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v(10);
// Populate the vector with random values
generate(v.begin(), v.end(), []() {
return rand() % 100;
});
for (auto i : v)
cout << i << " ";
return 0;
}
Output83 86 77 15 93 35 86 92 49 21
Using fill_n() with Random Values
The fill_n() function can also be used in combination with a random number generator in a similar way as generate() function.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v;
// Fill vector with 10 random values
fill_n(back_inserter(v), 10, rand() % 100);
for (auto i : v)
cout << i << " ";
return 0;
}
Output83 83 83 83 83 83 83 83 83 83
Similar Reads
How to Initialize a Vector with Default Values in C++? Initialization is the process of assigning the value to the elements of vector. In this article, we will learn how to initialize the vector with default value in C++.The simplest method to initialize the vector with default value is by using constructor during declaration. Letâs take a look at a sim
2 min read
How to Initialize a Vector with Values between a Range in C++? In C++, vectors are dynamic containers that can resize automatically when elements are inserted or deleted during the runtime. In this article, we will learn how to initialize a vector with a range of values in C++. Example: Input: start = 1, end =5 Output: myVector: {1, 5, 3, 4} Initialize a Vector
2 min read
How to Generate Random Value by Dice Roll in C++? In C++, we can simulate a dice roll by generating random numbers within a specific range. In this article, we will learn how to generate random values by dice roll in C++. Example: Input: Roll a diceOutput:Dice roll result is: 4Generating Random Values Similar to a Dice RollTo simulate a dice roll,
2 min read
How to Store Vectors as Values in a Map? In C++, vectors are similar to arrays, but unlike arrays, the vectors are dynamic in nature. They can modify their size during the insertion or deletion of elements. On the other hand, Maps are containers that allow the users to store data in the form of key-value pairs. In this article, we will lea
2 min read
How to Use a Range-Based for Loop with a Vector in C++? C++ introduced the range-based for loop as a convenient way to iterate over elements in a container. In this article, we'll look at how to iterate over a vector using a range-based loop in C++. Example Input:myVector: {1, 2, 3, 4, 5}Output:1 2 3 4 5Range-Based for Loop with Vector in C++The syntax o
1 min read