728x90
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

class Random {
public:
	Random();
	int next();
	int nextInRange(int a, int b);
};

Random::Random() { srand((unsigned)time(0)); }

int Random::next() {
	return rand();
}

int Random::nextInRange(int a, int b) {
	return (rand() % (b - a + 1) + a);
}

int main() {
	Random r;
	cout << "< 랜덤한 정수를 10개 출력하는 프로그램입니다 >\n";

	cout << "\n-- 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;
}

반응형

'전공 공부 > C++' 카테고리의 다른 글

문자열에서 a의 개수 출력  (0) 2021.01.03
클래스 Exp (베이스, 지수)  (0) 2021.01.02
클래스 Sample (프렌드 함수)  (0) 2021.01.02
배열의 합 (함수 중복)  (0) 2021.01.02
클래스 MyVector (생성자, 소멸자)  (0) 2021.01.01
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기