1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #include <iostream> #include <cstdlib> #include <ctime> using namespace std; class Random { public: Random() { srand((unsigned)time(0)); } int next(); int nextInRange(int low, int high); }; int Random::next() { int number = rand(); return number; } int Random::nextInRange(int low, int high) { int number = rand() % (high - low + 1) + low; return number; } int main() { Random r; cout << "-- 0에서 " << RAND_MAX << "까지의 랜덤 정수 10 개--" << endl; for (int i = 0; i < 10; i++) { int n = r.next(); cout << n << ' '; } cout << endl << endl << "-- 2에서 " << "4 까지의랜덤 정수 10 개 --" << endl; for (int i = 0; i < 10; i++) { int n = r.nextInRange(2, 4); cout << n << ' '; } cout << endl; } | cs |
'Programming > 명품 C++ Programming' 카테고리의 다른 글
명품 C++ Programming 실습문제 3장 5번 (0) | 2018.04.28 |
---|---|
명품 C++ Programming 실습문제 3장 4번 (0) | 2018.04.28 |
명품 C++ Programming 실습문제 3장 2번 (0) | 2018.04.28 |
명품 C++ Programming 실습문제 3장 1번 (0) | 2018.04.28 |
명품 C++ Programming 3장 Open Challenge (0) | 2018.04.28 |