我正在使用Windows / MinGW。我有以下代码可以在需要时生成随机范围:
random_device rd;
mt19937 eng(rd());
int get_random_int(int from, int to){
uniform_int_distribution<int> dist(from, to);
return dist(rd);
}
如何使用当前时间或其他随机种子为引擎提供动力? (我试图用时间代替rd,但它不可能)。在我的游戏中,敌人在各个方向的随机位置产生并开始向你移动。碰巧的是,每次我开始游戏时,它们都会在相同的地方产生(而且这与我想要的完全相反,因为它们会让人觉得可以预测)。
编辑(生成敌人的程序):
void generate_enemies(int q, int speed) {
for (int i = 0; i < q; i++) {
Actor a;
a.facing = get_random_int(0, 3);
a.speed = speed;
if (a.facing == UP) {
a.x = get_random_int(0, SCREEN_WIDTH);
a.y = -get_random_int(SCREEN_HEIGHT, SCREEN_HEIGHT + get_random_int(0, ENEMY_DISTANCE_FACTOR * get_random_int(0, ENEMY_DISTANCE_MULTIPLICATIVE_FACTOR)));
} else if (a.facing == DOWN) {
a.x = get_random_int(0, SCREEN_WIDTH);
a.y = get_random_int(SCREEN_HEIGHT, SCREEN_HEIGHT + get_random_int(0, ENEMY_DISTANCE_FACTOR * get_random_int(0, ENEMY_DISTANCE_MULTIPLICATIVE_FACTOR)));
} else if (a.facing == LEFT) {
a.x = -get_random_int(SCREEN_WIDTH, SCREEN_WIDTH + get_random_int(0, ENEMY_DISTANCE_FACTOR * get_random_int(0, ENEMY_DISTANCE_MULTIPLICATIVE_FACTOR)));
a.y = get_random_int(0, SCREEN_HEIGHT);
} else if (a.facing == RIGHT) {
a.x = get_random_int(SCREEN_WIDTH, SCREEN_WIDTH + get_random_int(0, ENEMY_DISTANCE_FACTOR * get_random_int(0, ENEMY_DISTANCE_MULTIPLICATIVE_FACTOR)));
a.y = get_random_int(0, SCREEN_HEIGHT);
}
enemies.push_back(a);
}
}