-1

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?

Conor Watson
  • 567
  • 1
  • 7
  • 28
  • 1
    Is there any reason you can't use `C++11` and the random number generators that come with that? – Galik Mar 25 '16 at 18:51
  • If `rand()` is a linear congruential generator, similar seeds can give similar initial values. The solution is simply to run the generator a dozen or so times, so that the values diverge more. – Pete Becker Mar 25 '16 at 18:51
  • Thanks for the reply guys. @Galik I didn't know C++11 had random number generators, I'll have a look into them. – Conor Watson Mar 25 '16 at 19:10
  • Possible duplicate of [Is there an alternative to using time to seed a random number generation?](http://stackoverflow.com/questions/7617587/is-there-an-alternative-to-using-time-to-seed-a-random-number-generation) – indiv Mar 25 '16 at 19:33
  • I don't understand what you're doing. If you want the user to enter the seed, why are you generating a random seed? If you want a random seed, why don't you just use `time(0)`? – Barmar Mar 25 '16 at 19:46
  • @Bamar Sorry probably explained that wrongly. The user is free the enter their own seed, however they also have the option of a random seed if they don't wan't to enter one. I used time(NULL) for this already but because it simply returns the current time, the seed can become very 'samey'. I'd like to generate a seed that was vastly different each time, i.e 0 - 9999999999. – Conor Watson Mar 25 '16 at 21:05

1 Answers1

1

I ended up using the C++11 Uniform Distribution Random Number Generator to give me a value between 0 and 4,294,967,295 like so:

std::default_random_engine generator;
std::uniform_int_distribution<int> distribution;
generator.seed(chrono::steady_clock::now().time_since_epoch().count());
random = distribution(generator);
Conor Watson
  • 567
  • 1
  • 7
  • 28