再次随机化的问题

Problems with randomizing again

本文关键字:问题 随机化      更新时间:2023-10-16

请看下面的代码

#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

这是为什么呢?我已经尝试过了..而循环也是如此。一点好处都没有。没有什么可以检查我在案例陈述中给出的条件。请帮忙。

翻转你的 while-loops to do-while。 表达式对于检查也是无效的(除非您有意,否则它们与文本不匹配)。根据消息,这些州是:

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

因此,这些部分需要更改为

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

后一部分对于"运行"和"爬网"状态很重要。由于它们只能产生一个有效的结果状态,因此旋转 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;

这就剩下你自己检查一下了,我留给你。

解决方案:将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

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

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

C 中带有常量种子的 rand() 函数总是倾向于给出相同的"随机"值,无论你做什么。

更好的方法是编写自己的随机生成器函数并使用它

#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