为什么我的mt19937随机发电机给我可笑的结果? C ++ [英] Why is my mt19937 Random generator giving me ridiculous results? C++
问题描述
使用另一个项目,我们需要使用 mt19937
随机生成数字。我们应该根据网格的部分随机选择一个x和y坐标。例如,我的函数传递 minX
, maxX
, minY
, maxY
添加到函数。我的x坐标工作正常。我在测试运行时不断收到错误。有时它会运行10次没有问题,然后点击一个错误。我放入一些自我调试行来显示mt生成器实际生成的内容。像我说的,x工作正常,y有时。
以下是我的代码:
code> void DungeonLevel :: generateRoom(int minX,int maxX,int minY,int maxY){
mt19937 mt;
mt.seed(time(NULL));
//计算随机宽度和高度;这两个范围
//从4-13
int iRandomWidth = 4 +(mt()%10);
int iRandomHeight = 4 +(mt()%10);
//计算X和Y方向的起点
int iStartX;
iStartX = mt()%(maxX - iRandomWidth);
cout<< xStart:<< iStartX<< endl; // cout flag
while((iStartX> maxX)&&(iStartX> = 0)){
cout< xStart:<< iStartX << endl; // cout flag
iStartX = mt()%(maxX - iRandomWidth);
}
int iStartY = 0;
iStartY = mt()%(maxY - iRandomHeight);
cout<<yStart:<< iStartY<< endl; // cout flag
while((iStartY> maxY)){
cout<<yStart:<iStartY = (mt()%(maxY - iRandomHeight));
}
//通过x和y坐标迭代,以及
//将tile设置为空间tile
//单单ROOM
int x = iStartX; x <= iStartX + iRandomWidth; x ++){
for(int y = iStartY; y <= iStartY + iRandomHeight; y ++){
if(y == iStartY){
dungeonGrid [y] [x] =' - '
}
else if(iStartX == x){
dungeonGrid [y] [x] ='|';
}
else if(y ==(iStartY + iRandomHeight)){
dungeonGrid [y] [x] =' - ';
}
else if(x ==(iStartX + iRandomWidth)){
dungeonGrid [y] [x] ='|';
}
else {
dungeonGrid [y] [x] ='。';
}
}
}
}
想象出我在@haatschii的帮助下做的业余错误。
现在这么多了。 iStartY和iStartX没有设置为低于或等于零的数字的限制。我感觉如此愚蠢,没有抓住那个哈哈。我添加了另一个循环,以确保该值大于0.我还使得iStartX和iStartY值开始为maxX + 1和maxY + 1,以便他们将自动进入循环,生成大于0的解。 / p>
解决方案代码:
void DungeonLevel :: generateRoom minX,int maxX,int minY,int maxY){
mt19937 mt;
mt.seed(time(NULL));
//计算随机宽度和高度;这两个范围
//从4-13
int iRandomWidth = 4 +(mt()%10);
int iRandomHeight = 4 +(mt()%10);
int iStartX = maxX + 1; //自动地必须进入第二while循环
while((iStartX> maxX)&&(iStartX> = 0)){
while((maxX-iRandomWidth)<= 0 ){
iRandomHeight = 4 +(mt()%10); // make value> 0
}
iStartX = mt()%(maxX - iRandomWidth);
}
int iStartY = maxY + 1; //自动地进入第二个循环
while((iStartY> maxY)){
while((maxY - iRandomHeight)< = 0){
iRandomHeight = 4 + ()%10); //设置为有效值
}
iStartY = mt()%(maxY - iRandomHeight);
}
//通过x和y坐标迭代,以及
//将图块设置为空间图块
// SINGLE ROOM
for(int x = iStartX ; x <= iStartX + iRandomWidth; x ++){
for(int y = iStartY; y <= iStartY + iRandomHeight; y ++){
if(y == iStartY){
dungeonGrid [y] [x] =' - ';
}
else if(iStartX == x){
dungeonGrid [y] [x] ='|';
}
else if(y ==(iStartY + iRandomHeight)){
dungeonGrid [y] [x] =' - ';
}
else if(x ==(iStartX + iRandomWidth)){
dungeonGrid [y] [x] ='|'
}
else {
dungeonGrid [y] [x] ='。';
}
}
}
}
感谢提示家伙!
Working on another project and we're required to use the mt19937
for randomly generating numbers. We're supposed to have it randomly choose an x and y coordinate based on the section of a grid. For example, my function passes minX
, maxX
, minY
, maxY
to a function. My x coordinate works fine. I kept getting errors randomly upon test runs. Sometimes it'll run 10 times with no problem, then hits an error. I put in some self debug lines to display what the mt generator is actually generating. Like I said, x works fine and y does sometimes. It'll randomly give me a -3437892 or 9743903.
Here's my code:
void DungeonLevel::generateRoom(int minX,int maxX,int minY, int maxY){
mt19937 mt;
mt.seed( time(NULL) );
// Calculate random width and height; these both range
// from 4-13
int iRandomWidth = 4 + (mt() % 10);
int iRandomHeight = 4 + (mt() % 10);
// Calculate the start points in both X and Y directions
int iStartX;
iStartX = mt() % (maxX - iRandomWidth);
cout << "xStart: " << iStartX<<endl; //cout flag
while ((iStartX > maxX) && (iStartX >= 0)){
cout << "xStart: " << iStartX<<endl;//cout flag
iStartX = mt() % (maxX - iRandomWidth);
}
int iStartY = 0;
iStartY = mt() % (maxY - iRandomHeight);
cout<<"yStart: " <<iStartY<<endl; //cout flag
while ((iStartY > maxY)){
cout<<"yStart: " <<iStartY<<endl;//cout flag
iStartY = (mt() % (maxY - iRandomHeight));
}
// Iterate through both x and y coordinates, and
// set the tiles to room tiles
// SINGLE ROOM
for( int x = iStartX; x <= iStartX + iRandomWidth; x++ ){
for( int y = iStartY; y <= iStartY + iRandomHeight; y++ ){
if (y == iStartY){
dungeonGrid[y][x] = '-';
}
else if (iStartX == x){
dungeonGrid[y][x] = '|';
}
else if (y == (iStartY+iRandomHeight)){
dungeonGrid[y][x] = '-';
}
else if (x == (iStartX+iRandomWidth)){
dungeonGrid[y][x] = '|';
}
else {
dungeonGrid[y][x] = '.';
}
}
}
}
Figured out the amateur mistake I made with the help of @haatschii.
It makes so much sense now. iStartY and iStartX had no limitation for being set to a number below or equal to zero.. I feel so dumb for not catching that lol. I added another loop to make sure that the value was greater than 0. I also made the iStartX and iStartY values start out as maxX+1 and maxY+1 so that they would automatically enter the loop to generate a solution greater than 0.
Heres the solution code:
void DungeonLevel::generateRoom(int minX,int maxX,int minY, int maxY){
mt19937 mt;
mt.seed( time(NULL) );
// Calculate random width and height; these both range
// from 4-13
int iRandomWidth = 4 + (mt() % 10);
int iRandomHeight = 4 + (mt() % 10);
int iStartX = maxX+1; //automatically has to enter the second while loop
while ((iStartX > maxX) && (iStartX >= 0)){
while ((maxX - iRandomWidth) <= 0){
iRandomHeight = 4 + (mt() % 10); //makes value > 0
}
iStartX = mt() % (maxX - iRandomWidth);
}
int iStartY = maxY+1; //automatically has to enter the second loop
while ((iStartY > maxY)){
while ((maxY - iRandomHeight) <= 0){
iRandomHeight = 4 + (mt() % 10); //sets to valid value
}
iStartY = mt() % (maxY - iRandomHeight);
}
// Iterate through both x and y coordinates, and
// set the tiles to room tiles
// SINGLE ROOM
for( int x = iStartX; x <= iStartX + iRandomWidth; x++ ){
for( int y = iStartY; y <= iStartY + iRandomHeight; y++ ){
if (y == iStartY){
dungeonGrid[y][x] = '-';
}
else if (iStartX == x){
dungeonGrid[y][x] = '|';
}
else if (y == (iStartY+iRandomHeight)){
dungeonGrid[y][x] = '-';
}
else if (x == (iStartX+iRandomWidth)){
dungeonGrid[y][x] = '|';
}
else {
dungeonGrid[y][x] = '.';
}
}
}
}
Thanks for the tips guys!
这篇关于为什么我的mt19937随机发电机给我可笑的结果? C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!