请教牛友们一个问题
- //请教牛友们,为啥我这里10次生成的随机数组都是一样的啊,我想要10次都不一样的
- #include <time.h>
- #include <assert.h>
- #include <iostream>
- #include <vector>
- #include <ctime>
- using std::cout;
- using std::endl;
- using std::vector;
- class Solution
- {
- public:
- void generateRandomVector(vector<int> &vec, int maxSize, int maxValue, int minValue)
- {
- assert(maxValue > minValue);
- srand(time(NULL));
- int size = rand() % maxSize + 1;
- vec.reserve(maxSize);
- for (int i = 0; i < maxSize; ++i)
- {
- int data = rand() % (maxValue - minValue) + minValue + 1;
- vec.push_back(data);
- }
- }
- void display(vector<int> &vec)
- {
- for (int i = 0; i < vec.size(); ++i)
- {
- cout << vec[i] << " ";
- }
- cout << endl;
- }
- };
- int main()
- {
- Solution sol;
- int maxSize = 20;
- int maxValue = 50;
- int minValue = 10;
- vector<int> vec;
- srand(time(NULL));
- for (int i = 0; i < 10; ++i)
- {
- vec.clear();
- sol.generateRandomVector(vec, maxSize, maxValue, minValue);
- sol.display(vec);
- }
- system("pause");
- return 0;
- }