C++中生成隨機數:,需要用到的函數,一個是 rand(),該函數只返回一個偽隨機數。生成隨機數之前必須先調用 srand() 函數。
生成隨機數
#include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main () { int i,j; // 設置種子 srand( (unsigned)time( NULL ) ); /* 生成 10 個隨機數 */ for( i = 0; i < 10; i++ ) { // 生成實際的隨機數 j= rand(); cout <<"隨機數: " << j << endl; } getchar(); return 0; }
說明:
srand函數是隨機數發生器的初始化函數。
原型: void srand(unsigned seed);
用法:它需要提供一個種子,這個種子會對應一個隨機數,如果使用相同的種子后面的rand()函數會出現一樣的隨機數。如: srand(1); 直接使用 1 來初始化種子。不過為了防止隨機數每次重復,常常使用系統時間來初始化,即使用 time 函數來獲得系統時間,它的返回值為從 00:00:00 GMT, January 1, 1970 到現在所持續的秒數,然后將 time_t 型數據轉化為(unsigned)型再傳給 srand 函數,即: srand((unsigned) time(&t)); 還有一個經常用法,不需要定義time_t型t變量,即: srand((unsigned) time(NULL)); 直接傳入一個空指針,因為你的程序中往往並不需要經過參數獲得的t數據。
生成指定范圍的隨機數
第一種
#include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main () { int i,j; // 設置種子 srand( (unsigned)time( NULL ) ); /* 生成 10 個隨機數 */ for( i = 0; i < 10; i++ ) { // 生成實際的隨機數 j= rand(); cout <<"隨機數: " << j % 100 << endl; } getchar(); return 0; }
或者可以在宏定義中頂一個random(int number)函數:
#include <iostream> #include <ctime> #include <cstdlib> #define random(x)(rand()%x) using namespace std; int main () { int i,j; // 設置種子 srand( (unsigned)time( NULL ) ); /* 生成 10 個隨機數 */ for( i = 0; i < 10; i++ ) { // 生成實際的隨機數 cout <<"隨機數: " << random(100) << endl; } getchar(); return 0; }
第二種
使用 rand() 和 srand() 產生指定范圍內的隨機整數的方法:“模除+加法”的方法。如要產生 [m,n] 范圍內的隨機數 num,可用:int num=rand()%(n-m+1)+m;(即 rand()%[區間內數的個數]+[區間起點值])
例如:使用隨機數來發紅包:
#include <iostream> #include <ctime> #include <cstdlib> #include <iomanip> #include <math.h> using namespace std; int main() { int i, number; int best;//手氣最佳 float total; cout << "請輸入紅包金額:"; cin >> total; cout << "請輸入搶紅包人數:"; cin >> number; /* 生成隨機數 */ // 設置種子 srand((unsigned)time(NULL)); float a[1024];//保存每個人的隨機數。最多支持1024個人搶紅包。 float b[1024];//保存每個人獲得的紅包金額。 float suma = 0;//隨機數總和。 float sumb = 0;//紅包總和。 int max = 0; for (i = 0; i < number; i++) { // 生成實際的隨機數 a[i] = rand() % 100; if (a[i] > max){ max = a[i]; best = i;//獲取手氣最佳 } suma += a[i]; } for (i = 0; i < number - 1; i++) { b[i] = a[i] / suma * total;//按照隨機數計算每個人實際獲得的金額 sumb += round(b[i] * 100) / 100.0;//將紅包金額保留兩位小數 //輸出信息 cout << "第" << setiosflags(ios::right)<< setw(3) << i + 1 << "個人的紅包是:" << setiosflags(ios::right) << setw(6) << setiosflags(ios::fixed) << setprecision(2) << round(b[i] * 100) / 100.0 ; if (best == i){ cout << "(手氣最佳)" << endl; } else { cout << endl; } } //最后一人的紅包金額等於總金額減去前面的金額。 cout << "第" << setiosflags(ios::right)<< setw(3) << number << "個人的紅包是:" << setiosflags(ios::right) << setw(6) << setiosflags(ios::fixed) << setprecision(2) << round((total - sumb) * 100) / 100.0; if (best == number - 1){ cout << "(手氣最佳)" << endl; } else { cout << endl; } system("pause"); return 0; }
說明:
1、setprecision(n)與setiosflags(ios::fixed)
setprecision(n)與setiosflags(ios::fixed)合用,可以控制小數點右邊的數字個數。
2、setw(3)
setw(n) 設域寬為n個字符
這個控制符的意思是保證輸出寬度為n。
如:cout<<setw(3)<<1<<setw(3)<<10<<setw(3)<<100;
輸出結果為1 10100 (默認是右對齊)
當輸出長度大於3時(<<1000),setw(3)不起作用
setioflags(ios::fixed) 固定的浮點顯示
setioflags(ios::scientific) 指數表示
setiosflags(ios::left) 左對齊
setiosflags(ios::right) 右對齊
setiosflags(ios::skipws 忽略前導空白
setiosflags(ios::uppercase) 16進制數大寫輸出
setiosflags(ios::lowercase) 16進制小寫輸出
setiosflags(ios::showpoint) 強制顯示小數點
setiosflags(ios::showpos) 強制顯示符號
例如:
cout<<setiosflags(ios::fixed)<<setiosflags(ios::right)<<setprecision(2):輸出一個右對齊的小數點后兩位的浮點數。
setprecision(n):控制輸出流顯示浮點數的數字個數。
setiosflags(ios::fixed):用定點方式表示實數。
iso::right :在指定區域內右對齊輸出