RAND

Section: ¸®´ª½º ÇÁ·Î±×·¡¸Ó ¸Å´º¾ó (3)
Updated: 1995³â 5¿ù 18ÀÏ
Index
Return to Main Contents
 

À̸§

rand, srand - random ¼ö »ý¼º±â.  

»ç¿ë¹ý

#include <stdlib.h>

int rand(void);

void srand(unsigned int seed);
 

¼³¸í

rand() ÇÔ¼ö´Â 0°ú RAND_MAX»çÀÌÀÇ pseudo-random Á¤¼ö¸¦ ¸®ÅÏÇÑ´Ù.

srand() ÇÔ¼ö´Â ÀÎÀÚ¸¦ rand()°¡ ¸®ÅÏÇÏ´Â pseudo-random Á¤¼öÀÇ »õ·Î¿î ¿¬¼ÓµÈ ¼öµéÀ» À§ÇÑ Á¾ÀÚ·Î ¼³Á¤ÇÑ´Ù. ÀÌµé ¿¬¼ÓµÈ ¼öµéÀº °°Àº seed °ªÀ¸·Î srand()¸¦ È£ÃâÇÏ¿© ¹Ýº¹µÈ´Ù.

¸¸ÀÏ ¾î¶² seed °ªµµ Á¦°øµÇÁö ¾Ê´Â´Ù¸é, rand() ÇÔ¼ö´Â ÀÚµ¿ÀûÀ¸·Î 1 °ªÀ¸·Î Á¾ÀÚ°¡ µÈ´Ù.  

¹Ýȯ°ª

rand() ÇÔ¼ö´Â 0°ú RAND_MAX»çÀÌÀÇ °ªÀ» ¹ÝȯÇÑ´Ù. srand()´Â ¾î¶² °ªµµ ¹ÝȯÇÏÁö ¾Ê´Â´Ù.  

ÁÖÀÇ

Linux C ¶óÀ̺귯¸®ÀÇ rand()¿Í srand() ¹öÀüÀº random()¿Í srandom()ó·³ °°Àº random ¼ö »ý¼º±â¸¦ »ç¿ëÇÑ´Ù. ±×·¡¼­ lower-order bit µéÀº higher-order bitµé ¸¸Å­ ·£´ýÇÏ´Ù. ±×·¯³ª, ¿¹Àü rand() ÀÛµ¿¿¡¼­, lower-order bitµéÀº higher-order bitµéº¸´Ù ´ú ·£´ýÇß´Ù.

Numerical Recipes in C: The Art of Scientific Computing (William H. Press, Brian P. Flannery, Saul A. Teukolsky, William T. Vetterling; New York: Cambridge University Press, 1990 (1st ed, p. 207))¿¡, ´ÙÀ½°ú °°Àº ÁÖ¼®ÀÌ ÀÖ´Ù:

"If you want to generate a random integer between 1 and 10, you should always do it by

j=1+(int) (10.0*rand()/(RAND_MAX+1.0));

and never by anything resembling

j=1+((int) (1000000.0*rand()) % 10);

(which uses lower-order bits)."

Random-number »ý¼ºÀº ¸Å¿ì º¹ÀâÇÑ ÁÖÁ¦ÀÌ´Ù. Numerical Recipes in C Ã¥(À§¿¡ ¾ð±ÞÇÑ)Àº Chapter 7 (Random Numbers)¿¡¼­ random-number »ý¼º¿¡ °üÇÑ ½ÇÁ¦ÀûÀÎ ÈǸ¢ÇÑ Åä·ÐÀ» Á¦°øÇÑ´Ù.

´õ ÀÌ·ÐÀûÀÎ Åä·ÐÀ» ¿øÇϸé, ±íÀÌ ÀÖ°Ô ¸¹Àº ½ÇÁ¦ÀûÀÎ À̽´¸¦ ´Ù·ç°í ÀÖ´Â Donald E. KnuthÀÇ The Art of Computer Programming, volume 2 (Seminumerical Algorithms), 2nd ÆÇÀÇ Chapter 3 (Random Numbers)¸¦ Âü°íÇضó.; Reading, Massachusetts: Addison-Wesley Publishing Company, 1981.  

ȣȯ

SVID 3, BSD 4.3, ISO 9899  

°ü·Ã Ç׸ñ

random(3), srandom(3), initstate(3), setstate(3)  

¿ªÀÚ

Á¤°­ÈÆ <skyeyes@soback.kornet.net>, 2001³â 3¿ù 9ÀÏ


 

Index

À̸§
»ç¿ë¹ý
¼³¸í
¹Ýȯ°ª
ÁÖÀÇ
ȣȯ
°ü·Ã Ç׸ñ
¿ªÀÚ

This document was created by man2html, using the manual pages.
Time: 08:07:01 GMT, March 25, 2004