I am looking for a seeded random number generator that creates a pool of numbers as a context. It doesn’t have to be too good. It is used for a game, but it is important, that each instance of the Game Engine has its own pool of numbers, so that different game instances or even other parts of the game that use random numbers don’t break the deterministic character of the generated numbers.
Currently I am using rand() which obviously doesn’t have this feature.
Are there any c or objective-c generators that are capable of doing what I want?
Use
srandto set the seed, and then userand():The man page explicitly states that the sequence of pseudo-random numbers can be repeatable (and hence it is deterministic):
Edit (clarification):
Note that the man page states that
srand()is niether reentrant nor thread-safe.I assumed that by “different game instances” you meant different processes, in which case it is okay to use it.
However, if you plan on changing seeds within the same process, you won’t get the functionality that you want. In this case I recommend using
rand_r()instead. Take a look at this question for reference.