랜덤 실수, 랜덤 알파벳 , 범위가 지정된 정수
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class Random {
public:
static void seed() {srand((unsigned int)time(NULL));}
static int nextInt(int a, int b);
static char nextAlphabet();
static double nextDouble();
};
int Random::nextInt(int a , int b) {
return (int)((float)rand() / (float)RAND_MAX *(float)(b) + a);
}
char Random::nextAlphabet() {
int num = 91;
while((num>90 && num<97))
num = (int)((float)rand() / (float)RAND_MAX *(float)(57) + 65);
return num;
}
double Random::nextDouble(){
return ((float)rand() / (float)RAND_MAX );
}
int main() {
Random::seed();
cout << "1에서 100까지 랜덤한 정수 10개를 출력합니다" << endl;
for (int i = 0; i < 10; i++)
cout << Random::nextInt(1, 100) << ' ';
cout << endl;
cout << "알파벳을 랜덤하게 10개를 출력합니다" << endl;
for (int i = 0; i < 10; i++)
cout << Random::nextAlphabet() << ' '; cout << endl;
cout << "랜덤한 실수를 10개를 출력합니다" << endl;
for (int i = 0; i < 5; i++)
cout << Random::nextDouble() << ' ';
cout << endl;
for (int i = 0; i < 5; i++)
cout << Random::nextDouble() << ' ';
cout << endl;
}
'c++' 카테고리의 다른 글
명품 c++ 7장 연습문제 8번, 9번 (0) | 2020.05.05 |
---|---|
명품 c++ 프로그래밍 6장 연습문제 9번 (0) | 2020.04.28 |
명품 C++ 연습문제 3장 5번 (0) | 2020.04.27 |
c++ 매개변수로 const 를 쓰는 이유 (0) | 2020.04.21 |
명품 c++ programming 5장 연습문제 12번 (0) | 2020.04.20 |