본문 바로가기

System Programmings/C++

[C++] 로또 뽑기 (난수 발생 & setw()함수 사용법)

반응형
#include <iostream>
#include <ctime> // seed값을 시간으로 주기 위해 포함
#include <iomanip> // setw() 함수를 쓰기 위해 포함
using namespace std;

int main()
{
	// 시간을 seed로 가진 난수 발생 (한번만 선언한다)
	srand((unsigned)time(NULL));


	int cnt;
	cout << "당신이 뽑길 원하는 로또 개수를 선택";
	cin >> cnt;
	cout << cnt << "개 선택" << endl << endl;


	for(int i=0; i<cnt; i++)
	{
		cout << i+1 << " : ";
		cout << setw(3) << rand()%45+1 << " " << setw(3) << rand()%46+1 << " ";
		cout << setw(3) << rand()%46+1 << " " << setw(3) << rand()%46+1 << " ";
		cout << setw(3) << rand()%46+1 << " " << setw(3) << rand()%46+1 << endl;
	}
	return 0;
}
반응형