Tatu Portin <> writes:
> I need a pseudorandom algorithm that produces same results with same
> seed across different platforms. I'm not sure, but I think that
> standard rand() does produce different set of numbers on different
> platforms. I would be using only four distinct values,
> i.e. (rand() % 4).
>
> This algorithm would be used to generate a bitmap when a game starts,
> and if randomness is platform-variable, the game will look different
> on different platforms and this is not what I want.
Right, there's no requirement for all C implementations to use the
same algorithm, but there is a sample algorithm in the standard.
C99 7.20.2.2:
static unsigned long int next = 1;
int rand(void) // RAND_MAX assumed to be 32767
{
next = next * 1103515245 + 12345;
return (unsigned int)(next/65536) % 32768;
}
void srand(unsigned int seed)
{
next = seed;
}
If you change the names of the functions (so they don't conflict with
the standard functions), I *think* the above code will give you the
same sequence for the same seed on all platforms. (Don't take my word
for that; try it on all platforms you're interested in.)
--
Keith Thompson (The_Other_Keith)
kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.