● rand()함수에 대해 알아봅시다

c언어 rand() 함수

#include  <cstdio>
#include  <windows.h>
#include  <stdlib.h>
#include  <conio.h>
#include  <time.h>
void gotoxy(int x, int y) 
{ 
    COORD NewPos={x, y}; 
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),NewPos); 
} 

int main() 
{ 
    int a, b, i; 
    srand(time(NULL)); 

    a = rand() % 10; 
    printf("rand() % 10 = %d\n", a); 

    a = rand() % 100 + 1; 
    printf("rand() % 100 = %d\n", a);  

    a = rand() % 90 + 10; 
    printf("rand() % 90 + 10 = %d\n", a);  

    for(i=0;i<10;i++) 
    { 
         a = rand() % 10 + 1; 
         b = rand() % 10 + 1; 
         printf("%d + %d = %d\n", a, b, a+b); 
    } 
    return 0; 
} 

 

[ c언어 7-1 문제 ] gotoxy함수를 이용해서 화면에 0부터9까지 수를 출력하는데 위치를 rand()를 이용해서 출력해봅시다

Posted by 명문코딩컴퓨터
,