rand

난수를 생성합니다.보다 안전한 버전의이 함수를 사용할 수 있습니다 자세한 내용은 rand_s.

int rand( void );

반환 값

rand위에서 설명한 대로 난수를 반환합니다.없음 오류가 반환이 됩니다.

설명

rand 함수 반환 의사 난수 정수 범위 0 RAND_MAX (32767)입니다.사용은 srand 함수를 호출 하기 전에 의사 난수 번호 생성기 rand.

요구 사항

루틴

필수 헤더

rand

<stdlib.h>

추가 호환성 정보를 참조 하십시오. 호환성 소개에서 합니다.

예제

// crt_rand.c
// This program seeds the random-number generator
// with the time, then exercises the rand function.
//

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

void SimpleRandDemo( int n )
{
   // Print n random numbers.
   int i;
   for( i = 0; i < n; i++ )
      printf( "  %6d\n", rand() );
}

void RangedRandDemo( int range_min, int range_max, int n )
{
   // Generate random numbers in the half-closed interval
   // [range_min, range_max). In other words,
   // range_min <= random number < range_max
   int i;
   for ( i = 0; i < n; i++ )
   {
      int u = (double)rand() / (RAND_MAX + 1) * (range_max - range_min)
            + range_min;
      printf( "  %6d\n", u);
   }
}

int main( void )
{
   // Seed the random-number generator with the current time so that
   // the numbers will be different every time we run.
   srand( (unsigned)time( NULL ) );

   SimpleRandDemo( 10 );
   printf("\n");
   RangedRandDemo( -100, 100, 10 );
}
  

해당 .NET Framework 항목

System::Random 클래스

참고 항목

참조

부동 소수점 지원

srand

rand_s