C++

[C++] 명품 C++ 프로그래밍 실습문제 6장 7번

Hs’s Coding vlog 2023. 1. 10. 17:43

[C++]명품 C++ 프로그래밍 실습문제 6장 7번

[C++]명품 C++ programming 실습문제 6장 7번

명품 C++ 프로그래밍실습문제/연습문제 /C++

#include <iostream>
#include <string>
using namespace std;
class Random{
public:
    static void send() {srand((unsigned)time(0));} // 난수생성함수
    static int nextInt(int min=0, int max = 32767){
        int res = rand()%32767 + 0;
        return res;
    }
    static char nextAlphabet(){
        int i =rand()%2;//2로 나누면 0,1값이 랜덤으로 나온다
        char randchar1,randchar2;
        if(i == 0){
            randchar1 = rand()%26 + 'a';
            return randchar1;
        }
        else{
            randchar2 = rand()%26 + 'A';
            return randchar2;
        }
    }
    static double nextDouble(){
        double max=32767;
        double res = rand()/max; //여기 랜덤나오게 하는게 중요하다
        return res;
    }
};

int main() {
    
    cout << "1에서 100까지 랜덤한 정수 10개를 출력합니다." << endl;
    for(int i=0 ;i<10 ;i++){
        cout << Random::nextInt() << " ";
    }
    cout <<endl;
    for(int i=0 ;i<10 ;i++){
        cout << Random::nextAlphabet() << " ";
    }
    cout <<endl;
    for(int i=0 ;i<10 ;i++){
        cout << Random::nextDouble() << " ";
    }
    cout <<endl;
    
    return 0;
}