'programming/C_C++'에 해당되는 글 279건

  1. 2013.07.04 random 숫자 생성

반응형

start에서 final까지의 범위안에 있는 난수 생성

----------------------------------------------

srand( time(NULL) );

 

{ rand() % (final - start + 1) } + start

 

----------------------------------------------

 

 

대략적인 분포가 일정한 형태의 큰난수????

 

----------------------------------------------

int GeneratorRandomNumber(int niStartNum, int niFinalNum)
{
        int niRandNum = 0;
        unsigned int nuiRandSeed = 0;

        struct timeval curtime;

        /*** srand( time(NULL) );  ***/

        gettimeofday(&curtime);
        nuiRandSeed = (unsigned int)( (curtime.tv_sec % 1000 * 1000000) + (curtime.tv_usec % 1000000) );

        srand( nuiRandSeed );

        niRandNum = (double)rand() / (double)(RAND_MAX) * (double)(niFinalNum - niStartNum) + niStartNum;

        return niRandNum;

}

----------------------------------------------

반응형
Posted by 공간사랑
,