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 循环。一点都不好。没有什么可以检查我在案例陈述中给出的条件。请帮忙。

4

4 回答 4

2

翻转你的while循环来做while。表达式对于检查也是无效的(除非您的意图是它们与文本不匹配)。根据消息,这些州是:

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

所以需要将部分更改为

  1. 在检查之前生成新的随机数。和..
  2. 在达到有效生产之前不要离开。

后一部分对于 Run 和 Crawl 状态很重要。由于它们只能产生一个有效的结果状态,旋转 rand() 调用来寻找该值是没有意义的。只需设置新状态并再次循环。

关于上述(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;

这让你再检查自己,我留给你。

于 2012-11-17T08:40:02.450 回答
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
于 2012-11-17T08:42:16.670 回答
0

您还可以在状态机中计算您的下一步,如下所示:

...

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

...
于 2012-11-17T10:10:25.820 回答
-2

无论您做什么,具有恒定种子的 C 中的 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循环

于 2012-11-17T08:40:26.717 回答