vendredi 25 mars 2016

Produce a random seed to give to srand() - Not time(NULL)

I am currently writing a level generation program for the game Sokoban. I use the rand() function quite a lot to generate each level, and I thought that a nice feature would be for the user to be able to control the seed used for the generation, meaning that two people could enter the same seed and the generator would generate the same levels.

I have been using

srand(std::time(NULL));

to produce the seed which works fine and my program will generate the same levels with the same seed no problem. However, as std::time(NULL) return the system time in Milliseconds, it doesn't give very interesting or diverse numbers, i.e it would produce 1476894985, and then the next seed might be 1476897904.

To give some more interesting seeds I tried creating a random seed between 0 and 9999999999 using the following code:

typedef unsigned long long ull;

SokoGenerator::ull SokoGenerator::randomNumber(ull min, ull max, int divisor){
    ull number = rand() % (max - min + 1)+ min;

    while(number % divisor != 0){
        number = rand() % (max - min + 1) + min;
    }

    return number;
}


srand(std::time(0));
genSeed = randomNumber(0, 999999999); }
srand(genSeed);
//Generate Levels After this point

But this seems to produce similar results, except they are a lot smaller now, i.e 45789, 46389, 47958.

Is there a better way to generate a good random seed between 0 and 9999999999 to give to the srand() function?




Aucun commentaire:

Enregistrer un commentaire