因為公司需要寫了一個亂數產生測試條件的小程式,再此紀錄下來
1 int _tmain(int argc, _TCHAR* argv[]) 2 { 3 fstream file; 4 file.open("temp.csv", ios::out); 5 6 int min = 0, max = 0, num = 0; 7 cout << "please input max, min, num :" << endl; 8 cin >> max >> min >> num; 9 cout << "max = " << max << " min = " << min << " num = " << num << endl; 10 double rnd = (double)((rand() % 10) + 1) / 10; 11 int step_range = (max - min) / 10; 12 int i = 0; 13 vector<int> temp; 14 15 while (max > (step_range * i) + min){ 16 temp.push_back((step_range * i) + min); 17 i++; 18 } 19 i--; 20 int index = 0; 21 while (num){ 22 int value = (rand() % 100) + 1; 23 double x = max / ((double)(rand() % 10) + 1) / 10; 24 double res = value * x + temp[rand() % i + 1]; 25 while (res > max) 26 { 27 value = (rand() % 100) + 1; 28 x = max / ((double)(rand() % 10) + 1) / 10; 29 res = value * x + temp[rand() % i]; 30 } 31 file << index << "," << res << endl; 32 index++; 33 num--; 34 } 35 36 system("pause"); 37 return 0; 38 }
因為條件內有上下限的限制所以先把條件範圍歸類成幾個區塊,但是在C++裡頭array沒辦法動態調整大小所以我用Vector
動態調整新增我的條件範圍
while (max > (step_range * i) + min){ temp.push_back((step_range * i) + min); i++; }