"Procedural" c++ 控制台应用程序中的游乐场生成
"Procedural" playground generation in c++ console application
学习基础知识和更多 c++ 的冒险(完全菜鸟)。现在我为所有要访问的字段创建了一个游乐场数组,但是当涉及到 for 循环中的 rand() 函数时,它会重复数字,因此它不是随机生成的。解决这个问题的一些想法?请注意,我不是很喜欢 rand func。
提前致谢!
这是我的代码
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
//**SETTINGS**
//Häufigkeit von field_type
#define VILLAGE 5
#define TOWN 2
int main()
{
//** THE PLAYGROUND **
struct field {
bool status;
string field_type;
};
int x = 5;
int y = 5;
field playground[5][5];
//Playground Typen definition
int village_counter = 0;
int town_counter = 0;
int x2 = 0;
int y2 = 0;
for (int counter = 0; counter < x * y; counter++) {
int Nummer;
srand(time(NULL));
Nummer = rand() % 4 + 1; // generates always the same number!
switch (Nummer) {
case 1:
village_counter++;
if (VILLAGE >= village_counter) {
playground[x2][y2].field_type = "VILLAGE";
}
else {
goto a_switch;
}
break;
case 2:
town_counter++;
if (TOWN >= town_counter) {
playground[x2][y2].field_type = "TOWN";
}
else {
goto b_switch;
}
break;
case 3:
a_switch:
playground[x2][y2].field_type = "GRASSLAND";
break;
case 4:
b_switch:
playground[x2][y2].field_type = "FOREST";
break;
}
x2++;
if (x2 == x) {
x2 = 0;
y2++;
}
}
//For test usage of all Field's
x2 = 0;
y2 = 0;
for (int counter = 0; counter < x * y; counter++) {
cout << counter << ": Field_TYPE = " << playground[x2][y2].field_type << endl;
x2++;
if (x2 == x) {
x2 = 0;
y2++;
}
}
}
time
请求当前时间(以秒为单位)作为整数。如果您的程序执行时间少于一秒,那么您每次都会使用相同的值“播种”随机数生成器。伪随机数生成器(即您的 rand()
调用)return 基于您播种的此状态的确定性系列值。
正如@Scheff 所说,最好只播种一次(可能在程序开始时),然后在需要时调用 rand()
。请注意 srand
和 rand
是来自 C 的古老历史产物,你最好使用最新的 C++ 编译器附带的更现代的生成器
有关详细信息,请参阅 c++ random number generators 上的文档
学习基础知识和更多 c++ 的冒险(完全菜鸟)。现在我为所有要访问的字段创建了一个游乐场数组,但是当涉及到 for 循环中的 rand() 函数时,它会重复数字,因此它不是随机生成的。解决这个问题的一些想法?请注意,我不是很喜欢 rand func。 提前致谢!
这是我的代码
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
//**SETTINGS**
//Häufigkeit von field_type
#define VILLAGE 5
#define TOWN 2
int main()
{
//** THE PLAYGROUND **
struct field {
bool status;
string field_type;
};
int x = 5;
int y = 5;
field playground[5][5];
//Playground Typen definition
int village_counter = 0;
int town_counter = 0;
int x2 = 0;
int y2 = 0;
for (int counter = 0; counter < x * y; counter++) {
int Nummer;
srand(time(NULL));
Nummer = rand() % 4 + 1; // generates always the same number!
switch (Nummer) {
case 1:
village_counter++;
if (VILLAGE >= village_counter) {
playground[x2][y2].field_type = "VILLAGE";
}
else {
goto a_switch;
}
break;
case 2:
town_counter++;
if (TOWN >= town_counter) {
playground[x2][y2].field_type = "TOWN";
}
else {
goto b_switch;
}
break;
case 3:
a_switch:
playground[x2][y2].field_type = "GRASSLAND";
break;
case 4:
b_switch:
playground[x2][y2].field_type = "FOREST";
break;
}
x2++;
if (x2 == x) {
x2 = 0;
y2++;
}
}
//For test usage of all Field's
x2 = 0;
y2 = 0;
for (int counter = 0; counter < x * y; counter++) {
cout << counter << ": Field_TYPE = " << playground[x2][y2].field_type << endl;
x2++;
if (x2 == x) {
x2 = 0;
y2++;
}
}
}
time
请求当前时间(以秒为单位)作为整数。如果您的程序执行时间少于一秒,那么您每次都会使用相同的值“播种”随机数生成器。伪随机数生成器(即您的 rand()
调用)return 基于您播种的此状态的确定性系列值。
正如@Scheff 所说,最好只播种一次(可能在程序开始时),然后在需要时调用 rand()
。请注意 srand
和 rand
是来自 C 的古老历史产物,你最好使用最新的 C++ 编译器附带的更现代的生成器
有关详细信息,请参阅 c++ random number generators 上的文档