Interface Design - Random Number Library
Objectives
- Interface ¼³°è ¹æ¹ýÀ» °øºÎÇÑ´Ù.
- Pseudo-random number¸¦ ÀÌ¿ëÇÑ random
behavior simulation ¹æ¹ýÀ» ¹è¿î´Ù.
Interface´Â ´ÙÀ½ »çÇ×µéÀ» °í·ÁÇÏ¿©
¼³°èÇÑ´Ù.
- Unified : ÇÑ ÁÖÁ¦¿¡ ´ëÇØ ÅëÇÕÀûÀÌ°í
ÀÏ°ü¼ºÀÌ ÀÖ¾î¾ß ÇÑ´Ù. [Graphics package¿¡ sqrt functionÀ» Æ÷ÇÔÇÒ
ÇÊ¿ä ¾ø´Ù. sqrt functionÀº math library¿¡ Æ÷ÇԵǾî¾ß ÇÑ´Ù.]
- Simple : ÀÌÇØÇϱ⠽±°í ÀÌ¿ëÇϱâ
½±µµ·Ï ´Ü¼øÇÏ¿©¾ß ÇÑ´Ù. [ Package ±¸Çö¿¡ °üÇÑ ÀÚ¼¼ÇÑ ³»¿ëÀº ÀϹÝÀûÀ¸·Î
»ç¿ëÀÚ°¡ ¾Ë ÇÊ¿ä ¾ø´Ù. »ç¿ëÀÚ°¡ ¾Ë ÇÊ¿ä°¡ ¾ø´Â »çÇ×Àº »ç¿ëÀڷκÎÅÍ
°Ý¸®ÇÏ¿© (Information hiding) »ç¿ëÀÚ´Â Ãß»óÈ (abstraction) µÈ
´Ü¼øÇÑ interface·Î package functionµéÀ» »ç¿ëÇÒ ¼ö ÀÖ¾î¾ß ÇÑ´Ù.]
- Sufficient : »ç¿ëÀÚ°¡ ÀÀ¿ëÇÏ°íÀÚ
ÇÏ´Â ºÐ¾ß¿¡ ´ëÇÑ ÃæºÐÇÑ ±â´ÉµéÀ» Á¦°øÇÏ¿©¾ß ÇÑ´Ù.
- General : ´Ù¾çÇÑ ºÐ¾ß¿¡ ÀÀ¿ëµÉ
¼ö ÀÖµµ·Ï ÀϹÝÀûÀ̾î¾ß ÇÑ´Ù.
- Stable : ¾ÈÀü¼ºÀÌ ÀÖ¾î¾ß ÇÑ´Ù.
[Package ±¸Çö»óÀÇ º¯È°¡ interface¿¡ ¹ÌÄ¡´Â ¿µÇâÀ» ÃÖ¼ÒÈ ÇÑ´Ù.]
Random number generator
- ¹«ÀÛÀ§ÇÏ°í ¿¹ÃøÀÌ ºÒ°¡´ÉÇÑ ÇàÀ§ (random behavior)¸¦
¸ðÇüÈ (simulation) Çϱâ À§ÇØ random number¸¦ »ç¿ëÇÑ´Ù.
- ¿ÏÀüÇÑ random number´Â ¿¹ÃøÀÌ ºÒ°¡´ÉÇÏ°í Áֱ⼺À»
°®Áö ¾Ê¾Æ¾ß ÇÑ´Ù.
- ƯÁ¤ ¾Ë°í¸®ÁòÀ» ÀÌ¿ëÇÑ ÄÄÇ»Å͸¦ ÀÌ¿ëÇÑ ³¼ö »ý¼º±â´Â
À§ ¿ä±¸¸¦ ¿ÏÀüÈ÷ ¸¸Á·½Ãų ¼ö ¾ø´Ù. ±×·¯¹Ç·Î ÄÄÇ»ÅÍ·Î »ý¼ºÇÑ random
number¸¦ pseudo-random number¶ó ÇÑ´Ù.
- ANSI C library´Â ´ÙÀ½°ú °°Àº pseudo-random
number functionÀ» Æ÷ÇÔÇÑ´Ù.
int rand(void); // stdlib.h¿¡ prototypeÀÌ Á¤ÀǵǾî ÀÖÀ½.
- À§ functionÀº 0¿¡¼ RAND_MAX »çÀÌÀÇ
¼ö¸¦ return ÇÑ´Ù.
- RAND_MAX´Â 16 bit integer¸¦ »ç¿ëÇÏ´Â
ȯ°æ¿¡¼´Â 32767, 32 bit integer¸¦ »ç¿ëÇϴ ȯ°æ¿¡¼´Â 2147483647·Î
Á¤ÀǵǾî ÀÖ´Ù.
#define RAND_MAX 32767
¶Ç´Â
#define RAND_MAX 2147483647
¸¹Àº ÀÀ¿ë ºÐ¾ß¿¡¼ ƯÁ¤ ¹üÀ§ÀÇ random number¸¦ ÇÊ¿ä·Î ÇÑ´Ù
- µ¿ÀüÀ» ´øÁ® µ¿ÀüÀÇ ¾ÕµÚ¸éÀ» °áÁ¤ÇÏ´Â ÇàÀ§¸¦ ¸ð¹æÇϱâ À§Çؼ´Â µÎ °¡Áö »óÅ (head or tail)¸¦ °®´Â random
number
- ÁÖ»çÀ§¸¦ ´øÁ® ³ª¿À´Â ¼ö¸¦ simulateÇϱâ À§Çؼ´Â 1¿¡¼ 6 »çÀÌÀÇ Á¤¼ö
- ·ê·¿ gameÀ» simulate Çϱâ À§Çؼ´Â 0¿¡¼ 36 »çÀÌÀÇ Á¤¼ö
- È®·üÀ» ¸ð¹æÇϱâ À§Çؼ´Â 0¿¡¼ 1 »çÀÌÀÇ ½Ç¼ö
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);
¾Ë°í¸®Áò)
- rand function °á°ú °ªÀ» 0 º¸´Ù
°°°Å³ª Å©°í 1 º¸´Ù ÀÛÀº ½Ç¼ö d·Î º¯È¯ (normalization)
- d°¡ ÇØ´ç ¹üÀ§ÀÇ ¼ö¸¦ ¸ðµÎ Æ÷ÇÔÇÒ
¼ö ÀÖµµ·Ï È®´ë (scale)
- ¼Ò¼ýÁ¡ ÀÌÇÏ ÀÚ¸®¸¦ ¶³¾î¹ö¸²À¸·Î½á d¸¦ Á¤¼öÈ.
[d´Â 0¿¡¼ (high - low + 1) »çÀÌÀÇ ¼ö]
- d¸¦ ÇØ´ç ¹üÀ§·Î ¿Å±è (translate;
À̵¿)
À§ ¾Ë°í¸®ÁòÀ» ±¸ÇöÇÑ random number package source
code (random.c) ¿Í interface (random.h)
Á¤ÀÇ
Interface´Â
- Function prototypes
- Constant definitions
- Type definitions
µéÀ» Æ÷ÇÔÇÏ¿©¾ß ÇÑ´Ù.
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¿¡ ´ëÇÑ ´ÙÀ½ »çÇ×µéÀ»
°í·ÁÇØ º¼ °Í.
- Á¦°øµÇ´Â ±â´ÉµéÀÌ ÀÏ°ü¼ºÀÌ Àִ°¡?
- ÀÌÇØÇϱ⠽±°í »ç¿ëÇϱ⠽¬¿î°¡?
- ÃæºÐÇÑ ±â´ÉµéÀ» Á¦°øÇϴ°¡?
- ´Ù¾çÇÑ ºÐ¾ß¿¡ ÀÀ¿ëÇÒ ¼ö Àִ°¡?