A deterministic random number generator with context?

I am looking for a random number generator that creates a pool of numbers as a context. This should not be too good. It is used for the game, but it is important that each instance of the Game Engine has its own set of numbers, so that different game instances or even other parts of the game that use random numbers do not violate the deterministic character of the generated numbers.

I am currently using rand (), which obviously does not have this function.

Are there c or objective-c generators that can do what I want?

+6
source share
5 answers

Use srand to set the seed, and then use rand() :

 unsigned int seed = 10; /* Choose an arbitrary value for the seed */ int r; srand(seed); /* Set the seed */ r = rand(); /* Generate a random number */ 

The man page explicitly states that a sequence of pseudorandom numbers can be repeatable (and therefore it is deterministic):

The srand() function sets its argument as a seed for the new pseudo-random integer sequence returned by rand() . These sequences are repeated, calling srand() with the same initial value.


Change (clarification):
Notice that the man page states that srand() is niether reentrant or thread safe.
I suggested that by “different game examples” you meant different processes, in which case it’s normal to use.
However, if you plan to change seeds as part of the same process, you will not get the necessary functionality. In this case, I recommend using rand_r() instead. Take a look at this question for reference.

+7
source

You don't seem to need a “context” (whatever that means); instead, you are looking for a PRNG implementation where you can save and restore the current state. This is really possible with any PRNG implementation that you implement yourself (since you can always save state), while library functions may or may not give you access to the state.

For Linux and MacOS, they actually added rand_r in addition to rand - this is documented as a thread-safe, reentrant version of rand, but the “magic” behind it is simply that it takes a pointer to the current state instead of storing it in a static variable. Other random number functions, such as the drand48 family, seem to have versions with additional parameters, although I would need to read more to find out if it can be used to store state.

In any case, if you are "google" or "wikipedia" for a random number generator to realize yourself, making the "current state" an explicit parameter will be trivial.

+3
source

Perhaps you can use random() and setstate() . I myself have not used setstate() , but the manpage seems to indicate that it can do what you want ...

+2
source

The "obvious" PRNG to use is the drand48() family of functions. They allow you to provide 48 bits of status and even allow you to set the multiplier and constant used in the calculations.

+2
source

Any good PRNG library should be able to do this. The GNU Science Library provides support for generating random numbers using many different algorithms and from many probability distributions. Each call to gsl_rng_alloc sets up an independent random number generator with its own state, which you can use with gsl_rng_set . You probably want to use different seeds for different parts of the program, and depending on which PRNG algorithm you use, some specific seeds may not work very well. Copying and pasting a few digits from random.org is probably a good way to get seeds.

+2
source

Source: https://habr.com/ru/post/919967/


All Articles