![]() |
C++問題
#include <stdio.h>
#include <stdlib.h> int main() { int a,b; for(a=0;a<=7;a++) { b=rand()%42+1; if(b<=42&&b>=0) printf("%d\n",b); } system("pause"); return 0; } 題目為:在1~42數字裡隨機挑出7個 但要每次都不同 這個程式跑出來結果每次都一樣 請問要如何解決 :on_47::on_47: |
rand()
你的隨機種子是否都沒變啊 rand( (unsigned)time( NULL ) )可以嗎? |
引用:
本來想打數字,他回報說不能給予條件,只能打void 我試看看,感謝幫忙 |
引用:
你的問題應該是隨機種子的問題 |
引用:
#include <stdlib.h> int main() { int a,b; for(a=0;a<=7;a++) { b=rand((unsigned)time(NULL))%42+1; if(b<=42&&b>=0) printf("%d\n",b); } system("pause"); return 0; } 他編譯後說錯誤說:on_47: |
#include <stdio.h>
#include <stdlib.h> #include <time.h> int main() { int a,b; srand( (unsigned)time( NULL ) ); for(a=0;a<=7;a++) { b=rand()%42+1; if(b<=42&&b>=0) printf("%d\n",b); } system("pause"); return 0; } |
引用:
|
引用:
|
引用:
應當a=1是嗎? |
引用:
{ b=rand()%42+1; if(b<=42&&b>=0) printf("%d\n",b); } 以上程式跑了八次,不是嗎?(你寫的,你忘了?) for(a=0;a<=7;a++) 改為 for(a=0;a<7;a++) 就會執行七次。 |
引用:
第一次寫時跑出7個 後來看完你得再跑變8個!! :on_47::on_47::on_51::on_51: |
所有時間均為台北時間。現在的時間是 09:25 AM。 |
Powered by vBulletin® 版本 3.6.8
版權所有 ©2000 - 2025, Jelsoft Enterprises Ltd.
『服務條款』
* 有問題不知道該怎麼解決嗎?請聯絡本站的系統管理員 *