2012年10月16日 星期二

亂數的使用

//雙斜線後面表示為註解
#include <stdio.h>  //包含 printf function 的標頭檔
#include <stdlib.h> //包含 srand, rand function 的標頭檔
#include <time.h>   //包含 time function 的標頭檔
void main() {       //主程式開始
 int a;             //宣告一個整數integer的變數variable
 srand(time(NULL)); //產生每次變數均不同的效果;用 srand() 函數改變一開始的亂數值
 a=(rand()%100)+1;  //計算, 取 1-100 的亂數
 printf("The Random Number is %d .\n", a); //印出隨機數的字串
}                   //主程式結束

沒有留言:

張貼留言