全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1412
推到 Plurk!
推到 Facebook!

請問這個程式中srand()與rand()如何使用?

尚未結案
GEPCY
一般會員


發表:3
回覆:4
積分:1
註冊:2005-01-05

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-01-06 14:04:10 IP:218.168.xxx.xxx 未訂閱
/*取自吳勁華-資料結構-金禾資訊-chap3-1 stack   請各為大大幫助提供解釋     Q1:srand()    Q2:rand() */    #include  #include #include #include using namespace std; void Swap(int*,int*); void push(int stack[],int MAX,int val); int pop(int stack[]); int top=-1; int main() { int card[52],stack[52]={0}; int i,j,k=0; char ascVal; int style; srand((unsigned)time(NULL)); //<-----------Q1 for(i=0;i<52;i ) card[i]=i 1; cout<<"洗牌中...請稍後!"<=0) { style=stack[top]/13; //計算牌子花色 switch(style) //牌子花色圖示對應 { case 0: //梅花 ascVal=5; break; case 1: ascVal=4; break; case 2: ascVal=3; break; case 3: ascVal=6; break; } cout<<"["<=MAX-1) cout<<"[堆疊已經滿了]"<
GEPCY
一般會員


發表:3
回覆:4
積分:1
註冊:2005-01-05

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-01-06 15:32:34 IP:218.168.xxx.xxx 未訂閱
1.為什麼srand()沒有設定,rand()的值就不會跑? 2.與stdlib.h中的設定有關嗎? _CRTIMP void __cdecl srand (unsigned int); 3.在srand()括弧內的值設定如何影響rand()? 4.若是有建議的書籍歡迎提供小弟參考!! 發表人 - gepcy 於 2005/01/06 15:37:59
fffhghgjh
一般會員


發表:2
回覆:33
積分:17
註冊:2004-12-23

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-01-06 17:39:30 IP:61.220.xxx.xxx 未訂閱
void srand(unsigned seed);    Description    Initializes random number generator. The random number generator is reinitialized by calling srand with an argument value of 1. It can be set to a new starting point by calling srand with a given seed number. 這是給random的函數一個起始點 比較多的做法會取時間帶入 用來在rand(void)中能產生不同的亂數 如果不用srand可以用randmize(void) 也是可以取不同的亂數起點
GEPCY
一般會員


發表:3
回覆:4
積分:1
註冊:2005-01-05

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-01-06 18:30:00 IP:218.168.xxx.xxx 未訂閱
再請教各位大大: 1.time是指電腦當下的時間? 2.那所取的時間的單位是"秒"還是"微秒"? 3.srand((unsigned)time(NULL)) 其中NULL為何不可省去,且非大寫不可?
fffhghgjh
一般會員


發表:2
回覆:33
積分:17
註冊:2004-12-23

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-01-06 20:22:34 IP:61.220.xxx.xxx 未訂閱
time_t time(time_t *timer);    Description    Gets time of day.    time gives the current time, in  seconds, elapsed since 00:00:00 GMT, January 1, 1970, and stores that value in the location pointed to by timer, provided that timer is not a NULL pointer. 在time的function中 需要傳一個指標過去 也可以這樣用 time_t t; t = time(&t); 一樣會得到現在的時間 NULL是C 內定字==> NULL #define Description Null pointer constant that is compatible with any data object pointer. It is not compatible with function pointers. When a pointer is equivalent to NULL it is guaranteed not to point to any data object defined within the program.
GEPCY
一般會員


發表:3
回覆:4
積分:1
註冊:2005-01-05

發送簡訊給我
#6 引用回覆 回覆 發表時間:2005-01-07 13:35:11 IP:218.168.xxx.xxx 未訂閱
請問fffhghgjh能否推薦這方面的書籍資料給小弟
fffhghgjh
一般會員


發表:2
回覆:33
積分:17
註冊:2004-12-23

發送簡訊給我
#7 引用回覆 回覆 發表時間:2005-01-07 19:04:46 IP:61.220.xxx.xxx 未訂閱
推薦書籍方面 我不敢推薦 或許你可以找些基本的C/C 的書先上手 我看你一開始所說的書 好像類似"資料結構--Using C/C "之類的 先單純找介紹C/C 語言的書
系統時間:2024-06-02 19:20:07
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!