C++ 入門指南 4.01
練習 16.6 參考程式 - 練習產生擬隨機數
// 引入標準程式庫中相關的輸入、輸出程式
#include <iostream>
// 引入標準程式庫中的 cstdlib 及 ctime
#include <cstdlib> // srand(), rand()
#include <ctime> // time()
// cout 為 std 中的輸出物件
using std::cout;
// endl 為 std 中的斷行符號
using std::endl;
int main(void) {
// 初始化擬隨機數產生器
srand(time(NULL));
// 取得擬隨機數
for (int i = 0; i < 10; i++) {
cout << rand() % 99 << ", ";
}
cout << endl;
// 最後回傳 0 給作業系統
return 0;
}
/* 《程式語言教學誌》的範例程式
http://kaiching.org/
檔名:exercise1606.cxx
編譯:g++ exercise1606.cxx
執行:./a.out
功能:C++入門指南單元十六的練習
作者:張凱慶 */
回到練習題目