rand() - 乱数の生成

標準

標準/拡張機能 C/C++ 依存項目

ISO C
POSIX.1
XPG4
XPG4.2
C99
Single UNIX Specification、バージョン 3

両方  

形式

#include <stdlib.h>

int rand(void);

機能説明

0 ~ RAND_MAX の範囲の疑似乱数整数を生成します。rand() を呼び出して乱数生成プログラムに seed を設定する前には、srand() 関数を 使用してください。srand() を呼び出さない場合、デフォルトのシードは 1 です。

戻り値

計算値を戻します。

CELEBR02
⁄* CELEBR02                                      

   This example prints the first 10 random numbers generated.                   
                                                                                
 *⁄                                                                             
#include <stdlib.h>                                                             
#include <stdio.h>                                                              
                                                                                
int main(void)                                                                  
{                                                                               
   int x;                                                                       
                                                                                
   for (x = 1; x <= 10; x++)                                                    
      printf("iteration %d, rand=%d¥n", x, rand());                             
}                                                                               
出力:
iteration 1, rand=16838
iteration 2, rand=5758
iteration 3, rand=10113
iteration 4, rand=17515
iteration 5, rand=31051
iteration 6, rand=5627
iteration 7, rand=23010
iteration 8, rand=7419
iteration 9, rand=16212
iteration 10, rand=4086

関連情報