Interface Design - Random Number Library

Objectives

Interface´Â ´ÙÀ½ »çÇ×µéÀ» °í·ÁÇÏ¿© ¼³°èÇÑ´Ù.



Random number generator

            int rand(void);  // stdlib.h¿¡ prototypeÀÌ Á¤ÀǵǾî ÀÖÀ½.


#define  RAND_MAX  32767
¶Ç´Â
#define  RAND_MAX  2147483647


¸¹Àº ÀÀ¿ë ºÐ¾ß¿¡¼­ ƯÁ¤ ¹üÀ§ÀÇ random number¸¦ ÇÊ¿ä·Î ÇÑ´Ù

  • ANSI C rand functionÀ» ÀÌ¿ëÇÏ¿© À§ °æ¿ì ¸ðµÎ¸¦ ó¸®ÇÒ ¼ö ÀÖ´Â random number generation package¸¦ ¼³°èÇÒ ¼ö ÀÖ´Ù.


    µ¿Àü ´øÁö±âÀÇ °æ¿ì

    rand() <= RAND_MAX / 2 À̸é head  // rand() % 2 == 0 À» Á¶°Ç½ÄÀ¸·Î »ç¿ëÇÒ °æ¿ì´Â?
    else  tail
    
    #define  HEAD  0
    #define  TAIL  1
    
    int TossCoin(void)
    {
       if (rand() <= RAND_MAX / 2)
          return  HEAD;
       else
          return  TAIL;
    }
    
    
    

    ÁÖ»çÀ§ ´øÁö±âÀÇ °æ¿ì

    À߸øµÈ ¿¹ 1)

    int RollDie(void)
    {
       if (rand() < RAND_MAX / 6) return 1;
       else if (rand() >= RAND_MAX / 6 && rand() < RAND_MAX * 2 / 6) return 2;
       else if (rand() >= RAND_MAX * 2 / 6 && rand() < RAND_MAX * 3 / 6) return 3;
       else if (rand() >= RAND_MAX * 3 / 6 && rand() < RAND_MAX * 4 / 6) return 4;
       else if (rand() >= RAND_MAX * 4 / 6 && rand() < RAND_MAX * 5 / 6) return 5;
       else if (rand() >= RAND_MAX * 5 / 6 && rand() <= RAND_MAX) return 6;
    }
    
    
    

    À߸øµÈ ¿¹ 2)

    int RollDie(void)
    {
       if (rand() < RAND_MAX / 6) return 1;
       else if (rand() < RAND_MAX * 2 / 6) return 2;
       else if (rand() < RAND_MAX * 3 / 6) return 3;
       else if (rand() < RAND_MAX * 4 / 6) return 4;
       else if (rand() < RAND_MAX * 5 / 6) return 5;
       else return 6;
    }
    
    
    

    À߸øµÈ ¿¹ 3)

    int RollDie(void)
    {
       int r;
    
       r = rand();
       if (r < RAND_MAX / 6) return 1;
       else if (r < RAND_MAX * 2 / 6) return 2;
       else if (r < RAND_MAX * 3 / 6) return 3;
       else if (r < RAND_MAX * 4 / 6) return 4;
       else if (r < RAND_MAX * 5 / 6) return 5;
       else return 6;
    }
    
    
    

    ¿¹ 4)

    int RollDie(void)
    {
       int r;
    
       r = rand();
       if (r < RAND_MAX / 6) return 1;
       else if (r < RAND_MAX / 6 * 2) return 2;
       else if (r < RAND_MAX / 6 * 3) return 3;
       else if (r < RAND_MAX / 6 * 4) return 4;
       else if (r < RAND_MAX / 6 * 5) return 5;
       else return 6;
    }
    
    
    

    Generalized random number package design

  • ÀÓÀÇÀÇ µÎ Á¤¼ö low¿Í high »çÀÌÀÇ random number generator

    int RandomInteger(int low, int high);

    ¾Ë°í¸®Áò)

    1. rand function °á°ú °ªÀ» 0 º¸´Ù °°°Å³ª Å©°í 1 º¸´Ù ÀÛÀº ½Ç¼ö d·Î º¯È¯ (normalization)
    2. d°¡ ÇØ´ç ¹üÀ§ÀÇ ¼ö¸¦ ¸ðµÎ Æ÷ÇÔÇÒ ¼ö ÀÖµµ·Ï È®´ë (scale)
    3. ¼Ò¼ýÁ¡ ÀÌÇÏ ÀÚ¸®¸¦ ¶³¾î¹ö¸²À¸·Î½á d¸¦ Á¤¼öÈ­. [d´Â 0¿¡¼­ (high - low + 1) »çÀÌÀÇ ¼ö]
    4. d¸¦ ÇØ´ç ¹üÀ§·Î ¿Å±è (translate; À̵¿)

  • À§ ¾Ë°í¸®ÁòÀ» ±¸ÇöÇÑ random number package source code (random.c) ¿Í interface (random.h) Á¤ÀÇ

    Interface´Â

    µéÀ» Æ÷ÇÔÇÏ¿©¾ß ÇÑ´Ù.



    Constant definitions´Â º¸Åë #define ¹®À» »ç¿ëÇÑ´Ù.


    CÀÇ preprocessor ¸í·É

    #include                // compile ½Ã ÁöÁ¤µÈ file Æ÷ÇÔ
    #define         // ÇϳªÀÇ symbol·Î ±ä ¹®ÀÚ¿­ ´ëÄ¡
    
    
    

    ¿¹)

    #include "filename"
    #include <filename>
    
    #define  symbol  replacement text
    
    
    

  • ´ëÄ¡ ¹®ÀÚ¿­ÀÌ ¿©·¯ ÁÙ¿¡ °ÉÃÄ ÀÖÀ» °æ¿ì ¸¶Áö¸· ÇàÀ» Á¦¿ÜÇÑ °¢ Çà ³¡À» '\'·Î ³¡³¿.

    ¿¹)

    #define  MAX (A, B)  ((A) > (B) ? (A) : (B))
    x = MAX(p+q, r+s); ==> x = ((p+q) > (r+s) ? (p+q) : (r+s)); ·Î ´ëÄ¡
    
    
    

    À߸ø »ç¿ë ¿¹)

    MAX(i++, j++)
    #define  square(x)  x * x
    
    
    

    #undef ¿¡ ÀÇÇØ Á¤ÀÇ°¡ Ãë¼ÒµÉ ¼ö ÀÖ´Ù.



    Á¶°ÇºÎ Æ÷ÇÔ (Conditional inclusion)

    #ifndef  SYMBOL          ==> #if  !define(SYMBOL)
    # define  SYMBOL
    ...
    ...
    #endif
    
    
    #if  SYSTEM==SYSV
    ...
    #elif  SYSTEM== BSD
    ...
    #elif  SYSTEM==WINDOWS
    ...
    #else
    ...
    #endif
    
    
    
    

  • À§¿¡¼­ ÀÛ¼ºµÈ random number package¸¦ »ç¿ëÇϱâ À§Çؼ­´Â client programÀº random.h fileÀ» includeÇÏ°í random.cÀÇ object code¿Í link ½ÃÄÑ¾ß ÇÑ´Ù. (dicetest.c)

  • À§ random number generator´Â ¶È°°Àº ÃʱⰪ(seed)¿¡ ´ëÇØ °°Àº random number sequence¸¦ return ÇÑ´Ù. ±×·¯¹Ç·Î seed value¸¦ ¾Ë¸é ¹ß»ýµÇ´Â ¼öµéÀ» ¿¹ÃøÇÒ ¼ö ÀÖ´Ù.

  • ÀÓÀÇÀÇ seed °ªÀ» »ç¿ëÇÏ¿© randomizeÇÒ ¼ö ÀÖ´Ù. (ÀÌ °æ¿ì time functionÀ» ¸¹ÀÌ »ç¿ë)
    void Randomize(void)
    {
       srand((int) time(NULL));       // time.h¸¦ includeÇÏ¿©¾ß ÇÔ.
    }
    
    
    

    À§ÀÇ random number interface design¿¡ ´ëÇÑ ´ÙÀ½ »çÇ×µéÀ» °í·ÁÇØ º¼ °Í.