목적
  -  특정 범위의 단위 정수 난수를 생성할줄 안다.

문제
사용자 삽입 이미지

코드

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
 int i50, i60, i70, i80, i90, i, iRand;
 i50 = i60 = i70 = i80 = i90 = i = iRand = 0;

 srand((unsigned)time(0));
 rand();

 cout << "생성된 난수 :" << endl;

 while( i < 100)
 {
  iRand = rand() % 5;
  switch(iRand)
  {
   case 0:
    i50++;
    break;

   case 1:
    i60++;
    break;

   case 2:
    i70++;
    break;

   case 3:
    i80++;
    break;

   case 4:
    i90++;
    break;

   default:
    break;

  }

  cout << (iRand + 5) * 10 << "  ";
  i++;

 }

 cout << "50이 생성될 확률 : " << i50 << endl
  << "60이 생성될 확률 : " << i60 << endl
  << "70이 생성될 확률 : " << i70 << endl
  << "80이 생성될 확률 : " << i80 << endl
  << "90이 생성될 확률 : " << i90 << endl;

 return 0;
}

해설
  - 예전 문제에서 범위만 바꾸어서 나온 것입니다. 0부터 시작하는 정수에서 확률을 계산해도 결과는 같다는 것을 이용했습니다.

참고
  - 유사 문제

Posted by 카켈



목적
  - 난수를 만들고 생성 확률을 계산한다.

문제
사용자 삽입 이미지

코드

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main(void)
{
 srand((unsigned)time(0));
 rand();

 int iRand, i10, i20, i30, i40, i50, i;
 iRand = i10 = i20 = i30 = i40 = i50 = i = 0;

 while(i <= 100)
 {
  iRand = rand() % 5 + 1;
 
  switch(iRand)
  {
   case 1:
    i10++;
    break;

   case 2:
    i20++;
    break;

   case 3:
    i30++;
    break;

   case 4:
    i40++;
    break;

   case 5:
    i50++;
    break;

   default:
    break;

  }

  cout << iRand * 10 << "  ";
  i++;

 }

 cout << endl << endl << "10이 나올 확률 : " << i10 / 100.0 << endl;
 cout << "20이 나올 확률 : " << i20 / 100.0 << endl;
 cout << "30이 나올 확률 : " << i30 / 100.0 << endl;
 cout << "40이 나올 확률 : " << i40 / 100.0 << endl;
 cout << "50이 나올 확률 : " << i50 / 100.0 << endl;

 return 0;
}

해설
  - rand() % 5 을 통해 0 부터 4까지 + 1 을 통해 1 부터 6까지 출력합니다.
  - switch-case 문으로 확률을 더하고 출력은 10배를 하여 원하는 값을 만들어 냅니다.
  - 10 ~ 50 가 10의 단위로 나올 확률과 1 ~ 5 가 나올 확률은 동일합니다.

참고
  - 유사 문제

Posted by 카켈




1. 목적
  - 원하는 횟수 만큼 특정 범위의 난수를 난수를 만들줄 안다.

2. 문제 (점수 : 20 점)
  - 1부터 4까지의 정수를 가지는 난수를 100개 생성하여 그 확률을 출력하라
  - switch-case 문을 사용 하여라 (if-else 문 사용하지 말것)
  - 출력 화면

1이 나올 확률 : 0.230
2가 나올 확률 : 0.230
3이 나올 확률 : 0.180
4가 나올 확률 : 0.360
Press any key to continue

3. 이해
  - 난수 생성 원리를 아는가?
  - 필요한 확률을 출력할수 있는가?

4. 코드

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
 float one, two, three, four;
 int gen, i;
 one = two = three = four = 0.0;
 gen = i = 0;

 srand((unsigned)time(NULL));

 while(i<100)
 {
  gen = rand() % 4 + 1;
  switch(gen)
  { 
   case 1:
    one++;break;

   case 2:
    two++;break;

   case 3:
    three++;break;

   case 4:
    four++;break;

   default:
    break;

  }

  i++;
 }
 
 printf("1이 나올 확률 : %0.3f\n2가 나올 확률 : %0.3f\n3이 나올 확률 : %0.3f\n4가 나올 확률 : %0.3f\n", one/100, two/100, three/100, four/100);

 return 0;
}

5. 해설
  - switch-case 문을 써서 특정 확률을 계산 할수 있습니다.
  - float 형 자료를 써서 소숫점 출력을 할수 있습니다.
  - 100개가 계산 되기에 한번도 나오지 않을 확률을 0으로 했습니다. 나온다면 오류가 납니다.

6. 참고
  - 유사 문제

Posted by 카켈




1. 목적
  - 무작위 숫자를 만들어 그 갯수를 확인한다.

2. 문제 (점수 : 40점)
  - 1부터 6사이의 임의의 정수 100개를 생성 각 숫자가 나타나는 확률을 계산하는 프로그램
  - 정수를 출력할 필요는 없다.
  - 중첩된 if-else 문 사용 / switch-case 문 사용 가능 (여기서는 switch-case 문으로 풀이)
  - 실행 화면

1 이 나올 확률 : 0.150
2 가 나올 확률 : 0.190
3 이 나올 확률 : 0.150
4 가 나올 확률 : 0.210
5 가 나올 확률 : 0.200
6 이 나올 확률 : 0.100
Press any key to continue


3. 이해
  - 원하는 범위의 난수를 잘 만들줄 아는가?
  - if-else / switch-case 문을 잘 활용할 줄 아는가?
  - 확률을 낼줄 아는가?

4. 코드

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
 
 int in = 0;
 int i = 0;
 int one = 0;
 int two = 0;
 int three = 0;
 int four = 0;
 int five = 0;
 int six = 0;

 srand((unsigned)time(NULL));
 
 while(i<100)
 {
  in = (rand() % 6) + 1;
  switch(in)
  {
   case 1:
    one++;
    break;

   case 2:
    two++;
    break;

   case 3:
    three++;
    break;

   case 4:
    four++;
    break;

   case 5:
    five++;
    break;

   case 6:
    six++;
    break;

   default:
    rand();

    break;

  }

  i++;
 }

 printf("1 이 나올 확률 : %0.3f\n", one == 0 ? 0 : (float)one/100 );
 printf("2 가 나올 확률 : %0.3f\n", two == 0 ? 0 : (float)two/100 );
 printf("3 이 나올 확률 : %0.3f\n", three == 0 ? 0 : (float)three/100 );
 printf("4 가 나올 확률 : %0.3f\n", four == 0 ? 0 : (float)four/100 );
 printf("5 가 나올 확률 : %0.3f\n", five == 0 ? 0 : (float)five/100 );
 printf("6 이 나올 확률 : %0.3f\n", six == 0 ? 0 : (float)six/100 );
 return 0;
}

5. 해설
  - 확률이 드물게 0 이 된다면 100으로 나눌때 오류가 나는걸 방지 하기 위해 간단한 조건문을 작성
    했습니다.
  - 선언된 변수가 int 형이기 때문에 출력시 소숫점 출력을 위해 float 형으로 형변환(casting)을 했습
    니다.
  - 0 ~ 32797 을 6으로 나눈 나머지 0 ~ 5 까지의 숫자에서 1을 더해 1 ~ 6 까지의 나올수 있는 숫자
    범위를 구했습니다.
  - if-else 문은 switch-case 문보다 더 자세한 경우를 다룰수 있지만 이 경우 처럼 숫자가 제한적
    인 경우 훨씬 세련된 코드로 보일수 있습니다.
  - srand((unsigned)time(NULL)) 구문은 첫부분에서 선언한 이상 추가 선언시 난수 생성시 예상치
    못한 경과가 생길수 있습니다.

6. 참고
  - http://www.winapi.co.kr/clec/cpp1/8-2-1.htm : rand() : 내부 검색
  - http://www.winapi.co.kr/clec/cpp1/9-3-4.htm : switch-case 구문

Posted by 카켈
이전페이지 1 다음페이지