【发布时间】:2015-10-05 17:57:27
【问题描述】:
我是 C++ 的新用户,目前我正在 YouTube 上尝试制作 1d 控制台提示游戏的挑战;我正在努力做到这一点,以便我拥有具有不同伤害范围的武器,并且当控制台打开时,它会给出一个设定范围之间的随机数,例如 40 - 80。
但是,当我尝试运行它以查看到目前为止的数据时,数字生成器只会一遍又一遍地给我相同的数字。现在,我将猜测并认为我的种子生成器有问题,但如果我将函数中的代码粘贴到 main 中,一切都会顺利运行。 std::mt19937 和 std::uniform_int_distribution 可以带参数进入函数还是我完全错误,返回错误?
我会发布一张图片,但由于我是 stackOverflow 的新手并且有
示例 1:
#include <iostream>
#include <string>
#include <random>
#include <ctime>
#include <stdlib.h>
// global veriables, health points, Alive status, random number generator and weapon damage values;
//health points
double giantHP = 2000;
double dwarfHP = 140;
double elfHP = 90;
double ironDwarf = 280;
double rydelTheArcher = 180;
// isMainAlive?
bool isRydelTheArcher = true;
bool isIronDwarf = true;
// random number function
int rN(int low, int high)
{
std::mt19937 seed(time(NULL));
std::uniform_int_distribution<int> attackNum(low, high);
return attackNum(seed);
}
// weapon damage with min and max values
int club = rN(40, 80); int fist = rN(25, 50);
int arrow = rN(4, 12); int fireArrow = rN(8, 24); int rydelsArrows = rN(8, 24);
int lrgAxe = rN(24, 28); int medAxe = rN(15, 20); int smallAxe = rN(10, 15); int ironDwarfAxe = rN(40, 60);
int main()
{
/***************************************************************************************************************
****************************************************************************************************************
** create variables, health points for Giants (attackers), Dwarfs and Elves (defenders) / ( ALT + 0124 = || ),**
** damage taken, damage dealt, **
** speed, attack speed **
** and weapons used & range of attack **
****************************************************************************************************************
****************************************************************************************************************/
// title
std::cout << " *** Giants vs Dwarfs and Elves *** " << std::endl;
// weapon count for loop
// testing to see if numbers are being outputted in a random way
int weaponHitCount = 0;
for (weaponHitCount = 0; weaponHitCount < 5000; weaponHitCount++)
{
std::cout << "damageDealt: " << club << std::endl
<< fist << std::endl
<< arrow << std::endl
<< fireArrow << std::endl
<< rydelsArrows << std::endl
<< lrgAxe << std::endl
<< medAxe << std::endl
<< smallAxe << std::endl
<< ironDwarfAxe << std::endl
<< "!*----------------------------------------------------------------------------------*!" << std::endl;
}
std::cin.get();
std::cin.ignore();
return 0;
}
/* (weaponHitCount = 0; weaponHitCount <= 40000; weaponHitCount++)
{
int newWeapon = randomNumber(40, 80);
std::cout << "weapon Damage equals: " << newWeapon << " " << std::endl;
}*/
示例 2:
#include <iostream>
#include <random>
#include <stdlib.h>
#include <string>
#include <ctime>
/** weapon damage with min and max values
**int club = rN(40, 80); int fist = rN(25, 50); // Giants
**int arrow = rN(4, 12); int fireArrow = rN(8, 24); int rydelsArrows = rN(8, 24); // Elves
**int lrgAxe = rN(24, 28); int medAxe = rN(15, 20); int smallAxe = rN(10, 15); int ironDwarfAxe = rN(40, 60);*/ // Dwarves
int rN(int min, int max) // random numbers for each weapon
{
std::mt19937_64 seeder(time(NULL));
std::uniform_int_distribution<int> club(min = 40, max = 80);
std::uniform_int_distribution<int> fist(min = 25, max = 50);
std::uniform_int_distribution<int> arrow(min = 4, max = 12);
std::uniform_int_distribution<int> fireArrow(min = 8, max = 24);
std::uniform_int_distribution<int> rydelsArrows(min = 16, max = 26);
std::uniform_int_distribution<int> lrgAxe(min = 24, max = 28);
std::uniform_int_distribution<int> medAxe(min = 15, max = 20);
std::uniform_int_distribution<int> smallAxe(min = 10, max = 15);
std::uniform_int_distribution<int> ironDwarfAxe(min = 40, max = 60);
return club(seeder), fist(seeder),
arrow(seeder), fireArrow(seeder),
rydelsArrows(seeder), lrgAxe(seeder),
medAxe(seeder), smallAxe(seeder),
ironDwarfAxe(seeder);
}
int main()
{
/***************************************************************************************************************
****************************************************************************************************************
** create variables, health points for Giants (attackers), Dwarfs and Elves (defenders) **
** damage taken, damage dealt, **
** speed, attack speed **
** and weapons used & range of attack **
****************************************************************************************************************
****************************************************************************************************************/
std::cout << " *** Giants vs Dwarfs and Elves *** " << std::endl;
// random number output test
for (int c = 0; c <= 250; c++)
{
std::cout << "number: " << rN(40, 80) << std::endl;
std::cout << "--------" << std::endl;
}
std::cin.get();
std::cin.ignore();
}
任何帮助将不胜感激, 亲切的问候,谢恩。
【问题讨论】:
-
发布文字,而不是图片。
-
欢迎来到 Stack Overflow!请edit您的问题与minimal reproducible example 或SSCCE (Short, Self Contained, Correct Example)