How to Initialize a Vector with Values between a Range in C++? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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 with Values between a Range in C++We can initialize a vector with a range of values by using a loop and a random number generator srand() with modular arithmetic such that the number generated would be in between the upper and lower bound. ApproachCreate a random number generator object and use the current time as a seed for a random generator.Define the start and end of the range for the random numbers.Use a for loop with n iterations to generate random numbers within the given range and insert them into the vector. To make the number in the desired range, use the formula: start + rand() % (end - start + 1).C++ Program to Initialize a Vector with Values between a Range C++ // C++ Program to show how to Initialize a Vector with // Values between a Range #include <cstdlib> #include <ctime> #include <iostream> #include <vector> using namespace std; int main() { // Use current time as seed for random generator srand(time(0)); // Start and end of range int start = 1, end = 5; // Vector declaration vector<int> myVector; // Initializing the vector with a range of random values for (int i = start; i <= end; i++) { int randNum = start + rand() % (end - start + 1); myVector.push_back(randNum); } // Printing the vector cout << "Vector: "; for (int i : myVector) { cout << i << " "; } cout << endl; return 0; } OutputVector: 2 5 1 4 3 Time Complexity: O(N) where N is the number of elements.Auxiliary Space: O(N) Create Quiz Comment G gaurav472 Follow 0 Improve G gaurav472 Follow 0 Improve Article Tags : C++ Programs C++ STL cpp-vector cpp-random CPP Examples +2 More Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like