2012-11-17 100 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; 

...