0

伙计们,当我运行它时,我的程序遇到了这个非常奇怪的错误。这是重要的代码:

变量(编辑):

const short int maxX = 100;
const short int maxZ = 100;
const short int lakeChance = 0.9;

addLake 函数(编辑):

void addLake(Sect sector[][maxZ], int x, int z){    
    sector[x][z].setMaterial(1);
}

主要来源(编辑):

//Generating land mass
for (int x = 0; x < maxX; x++){
    for (int z = 0; z < maxZ; z++){
        //Default material
        sector[x][z].setMaterial(0);

        //Add lake/sea source
        int r = rand();
        float r1 = (r % 1000);
        float r2 = r1 / 1000;

        if (r2 <= lakeChance) addLake(sector, x, z);
    }
}

错误是,无论我将“lakeChance”值更改为什么,结果似乎都完全相同。正如在 addLake 函数中一样,尽管我将lakeChance 的值更改得更高(即:0.5、0.7、0.9),但每次启动似乎都会调用大约 3 到 6 次。只有当我将“lakeChance”的值更改为 1 时,才会一直调用该函数。随机数生成器工作正常,因此每次结果都会有所不同。

4

2 回答 2

4

我看不出本身有什么明显的错误。所以我尝试了以下代码:

int test(float chance)
{
    int call = 0;

    for(int i = 0; i != 100000; i++)
    {
        int r = rand();

        float r1 = (r % 1000);

        float r2 = r1 / 1000;

        if(r2 <= chance)
            call++;
    }

    cout << "Chance is " << chance << ": " << call << " invocations or "
         << ((100.0 * call) / 100000.0) << "% of the time!" << endl;

    return call;    
}

不出所料,我得到了这些结果:

Chance is 0.1: 10216 invocations or 10.216% of the time!
Chance is 0.2: 20232 invocations or 20.232% of the time!
Chance is 0.3: 30357 invocations or 30.357% of the time!
Chance is 0.4: 40226 invocations or 40.226% of the time!
Chance is 0.6: 60539 invocations or 60.539% of the time!
Chance is 0.7: 70539 invocations or 70.539% of the time!
Chance is 0.8: 80522 invocations or 80.522% of the time!
Chance is 0.9: 90336 invocations or 90.336% of the time!
Chance is 1: 100000 invocations or 100% of the time!

您要添加多少个湖泊?

于 2013-01-05T00:42:52.680 回答
-1

randomize()在调用之前尝试rand()

于 2013-01-05T00:26:48.507 回答