gpt4 book ai didi

c++ - 再次随机化的问题

转载 作者:太空宇宙 更新时间:2023-11-04 14:12:39 25 4
gpt4 key购买 nike

<分区>

请看下面的代码

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com