简体   繁体   English

C用嵌套函数生成随机数

[英]C generate random number with nested function

I can generate random number with srand, rand (by seeding).我可以用 srand、rand(通过播种)生成随机数。 If I just need to run in 1 particular function, it is okay.如果我只需要在 1 个特定函数中运行,那没关系。 But, I have code like this.但是,我有这样的代码。

int generateBattleShipX() {
      srand ( time(NULL) );
    return rand() % 14;
}

int generateBattleShipY() {
     srand ( time(NULL) );
    return rand() % 14;
}

int tryToFillBattleField(int X, int Y) {

if ((X >= 1 && X <= 13) && (Y >= 1 && Y <= 13)) //battle ship is in the field
{

    int lowerX = X - 1; 
    int upperX = X + 1;

    int lowerY = Y - 1; 
    int upperY = Y + 1;

    // printf("lowerX = %d upperX = %d lowerY = %d upperY = %d", lowerX, upperX, lowerY, upperY);
    int left = my_data[Y][lowerX];
    int top = my_data[lowerY][X];
    int right = my_data[Y][upperX];
    int bottom = my_data[upperY][X];

    if (left == 0 && top == 0 && right == 0 && bottom == 0) {
        my_data[Y][lowerX] = track;
        my_data[lowerY][X] = track;
        my_data[Y][upperX] = track;
        my_data[upperY][X] = track;
        my_data[Y][X] = track;
        track++;

        if (track > 10) {
            return 1; //stop
        }
        else {
              int x = generateBattleShipX();
              int y = generateBattleShipY();

            // printf("x = %d y = %d \n",x,y );
         tryToFillBattleField(x, y); //filling next battle ship
        }
    }
    else {
              int x = generateBattleShipX();
              int y = generateBattleShipY();

        tryToFillBattleField(x, y);//redoing because it overlap
    }
    return 0;
}
else {
    // redoing because battle ship is out of field..
              int x = generateBattleShipX();
              int y = generateBattleShipY();
            // printf("x = %d y = %d \n",x,y );

        tryToFillBattleField(x, y);
    return 0;
}
    return 0;
}

tryToFillBattleField is called by its method again and again about 10 times. tryToFillBattleField 被它的方法一次又一次地调用大约 10 次。 Problem is it can generate random number only for 1st time.问题是它只能第一次生成随机数。 For the rest, it is always the same.对于其余的,它总是一样的。 Is there a particular method I need to write for nested function to generate random number?是否需要为嵌套函数编写特定方法来生成随机数?

The way it is now, each time you call generateBattleShipX or generateBattleShipY you re-seed the random number generator.现在的情况是,每次您调用generateBattleShipXgenerateBattleShipY您都会重新播种随机数生成器。 Since time returns the time in seconds, multiple successive calls will return the same value, so the random number generator gets seeded with the same value, resulting in the same "random" number each time.由于time以秒为单位返回时间,因此多次连续调用将返回相同的值,因此随机数生成器使用相同的值作为种子,每次都会产生相同的“随机”数。

srand should only be called once at the start of your program to seed the random number generator. srand只应在程序开始时调用一次以播种随机数生成器。 Once you do that, you'll get more random results.一旦你这样做,你会得到更多的随机结果。

dbush is correct, however a better pseudo random number would be to use the encryption library (if you OS supports one) as the "randomness" of the random number generator is better than using time as your seed. dbush 是正确的,但是更好的伪随机数是使用加密库(如果您的操作系统支持),因为随机数生成器的“随机性”比使用时间作为种子要好。

As it stands, with the corrections, this would be easy to predict the numbers and defeat the "game."就目前而言,通过更正,这将很容易预测数字并击败“游戏”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM