2012-11-17 90 views
1

請看看下面的代碼再次與隨機問題

#include <iostream> 
#include <stdlib.h> 
#include <ctime> 

using namespace std; 

int main() 
{ 
    enum Movement{STAND=1,WALK,RUN,CRAWL}; 

    srand(time(0)); 
    Movement state = (Movement)(1+rand()%4); 

    for(int i=0;i<10;i++) 
    { 

    cout << state << endl; 

    switch(state) 
    { 
     /*Here the logic is, 
     * 
     * 1. From stand, he can walk or crawl 
      2. From Walk, he can stand or run 
      3. From Run, he can walk 
      4. From Crawl, he can stand 
     */ 

     case STAND: 
      cout << "You can walk or crawl" << endl;   
      while(state==WALK || state==CRAWL) 
      { 
       state = (Movement)(1+rand()%4); 
      } 
      break; 


     case WALK: 
      cout << "You can stand or run" << endl; 
      while(state==STAND || state==RUN) 
      { 
       state = (Movement)(1+rand()%4); 
      } 
      break; 


     case RUN: 
      cout << "You can walk" << endl; 
      while(state==WALK) 
      { 
       state = (Movement)(1+rand()%4); 
      } 
      break; 

     default: 
      cout << "You can stand" << endl; 
      while(state==STAND) 
      { 
       state = (Movement)(1+rand()%4); 
      } 

    } 

    } 
} 

我使用隨機的,並基於這些給定條件期望隨機結果。但是我得到的結果如下。

2 
You can stand or run 
2 
You can stand or run 
2 
You can stand or run 
2 
You can stand or run 
2 
You can stand or run 
2 
You can stand or run 
2 
You can stand or run 
2 
You can stand or run 
2 
You can stand or run 
2 
You can stand or run 

這是爲什麼?我已經嘗試過do..while循環。一點都不好。沒有什麼是檢查我在病例陳述中給出的條件。請幫忙。

+1

請學習如何使用調試器。 –

回答

2

翻轉你的while循環來做,而。表達式是無效的,以及爲檢查(除非它是你的意圖,他們不匹配的文本)。各州,根據消息是:

Stand ==> (Walk || Crawl) 
Walk ==> (Stand || Run) 
Run ==> (Walk) 
Crawl ==> (Stand) 

所以需要改變,以

  1. 的部分生成新的隨機數之前檢查它。和..
  2. 不離開,直到達到一個有效的生產。

後半部分是運行和抓取狀態非常重要的。因爲他們只能生產一種有效的結果狀態,紡蘭特()調用尋找一個價值是沒有意義的。只需設置新的狀態並再次循環。

關於上述(2):

case WALK: 
     cout << "You can stand or run" << endl; 
     while(state==STAND || state==RUN) 
     { 
      state = (Movement)(1+rand()%4); 
     } 
     break; 

變爲...

case WALK: 
     cout << "You can stand or run" << endl; 
     do { 
      state = (Movement)(1+rand()%4); 
     } while(state!=STAND && state!=RUN); 
     break; 

關於運行和抓取規定:

case RUN: 
     cout << "You can walk" << endl; 
     state = WALK; 
     break; 

    default: // CRAWL 
     cout << "You can stand" << endl; 
     state = STAND; 
     break; 

這讓你多一個檢查自己,我給你留下。與恆種子Ç

+0

謝謝!雖然也不需要,while循環中的條件是錯誤的!爲什麼我沒有看到!謝謝 –

+0

@Sepala如果你希望下一步是隨機的,你需要那些do-while循環(其中兩個,一個用於STAND,一個用於WALK)。其他人可以被淘汰。除了那些兩次做什麼之外,你不需要在循環中有一個rand-gen,這與回答這個問題的其他人的流行觀點相反。 – WhozCraig

+0

好的,非常感謝你:) –

-2

rand()函數總是傾向於給予相同的「隨機」值,不管你做什麼。

更好的方法是寫自己的RandomGenerator功能並用其

#include<stdio.h> 
#include<stdlib.h> 
#include<time.h> 

int RandomGenerator(int min, int max) // Generate min < random int < max 
{ 
    int randNum; 
    srand(rand()*time(NULL)); 
    randNum = rand() % max + min; 
    // printf(" Random number is %d \n", randNum); 
    return randNum; 
} 

而且移動你的Movement state = (Movement)(1+rand()%4);for

+0

是的,但他用時間()來使用srand()來避免這種情況。在程序開始時僅僅調用一次srand就是解決方案。不是重複調用的函數,randrand時間返回操作在 – fayyazkl

+0

srand(time(0))之上完成時,將始終使用相同的隨機值進行種子處理,並且每次執行都會返回相同的隨機數。 srand(rand()* time(null))給出更多的「隨機」結果。此外,代碼也存在隨代碼循環的問題 – Anshul

+1

而不是如果你只在程序開始時調用srand(time(0))一次並繼續調用rand,那麼你完全沒問題,並且得到一個隨機數每個蘭特呼叫。你對Rand在循環之外是正確的,但你沒有在解決方案中提到這一點。人們只是根據書面解決方案給予反饋 – fayyazkl

0

解決方案:移動Movement state = (Movement)(1+rand()%4);for循環。

更正代碼:

#include <iostream> 
#include <stdlib.h> 
#include <ctime> 

using namespace std; 

int main() 
{ 
    enum Movement{STAND=1,WALK,RUN,CRAWL}; 
    srand(time(0)); 

    for(int i=0;i<10;i++) 
    { 

     Movement state = (Movement)(1+rand()%4); 

     cout << state << endl; 

     switch(state) 
     { 
      /*Here the logic is, 
      * 
      * 1. From stand, he can walk or crawl 
       2. From Walk, he can stand or run 
       3. From Run, he can walk 
       4. From Crawl, he can stand 
      */ 

      case STAND: 
       cout << "You can walk or crawl" << endl;   
       while(state==WALK || state==CRAWL) 
       { 
        state = (Movement)(1+rand()%4); 
       } 
       break; 


      case WALK: 
       cout << "You can stand or run" << endl; 
       while(state==STAND || state==RUN) 
       { 
        state = (Movement)(1+rand()%4); 
       } 
       break; 


      case RUN: 
       cout << "You can walk" << endl; 
       while(state==WALK) 
       { 
        state = (Movement)(1+rand()%4); 
       } 
       break; 

      default: 
       cout << "You can stand" << endl; 
       while(state==STAND) 
       { 
        state = (Movement)(1+rand()%4); 
       } 

     } 

    } 

    return 0; 
} 

輸出:

3 
You can walk 
1 
You can walk or crawl 
2 
You can stand or run 
1 
You can walk or crawl 
2 
You can stand or run 
3 
You can walk 
4 
You can stand 
2 
You can stand or run 
2 
You can stand or run 
4 
You can stand 

Press any key to continue 
+0

這不會解決她的問題。她內在的核查過濾重複案件都沒有妥善執行。 *會*產生一連串隨機數,但那就是它。她想要的過濾將被跳過。 – WhozCraig

0

你也可以計算出你的狀態機這樣的下一個步驟:

... 

case STAND: 
    cout << "You can walk or crawl" << endl; 
    state = rand()%2 ? WALK : CRAWL; 
    break; 

...