John John - 6 months ago 15
Objective-C Question

How does one write a repeatable pseudo random number generator?

For example:

If

rprng(seed,index)
is my function, then for any pair of
(seed,index)
, I should always get the same value for a given
(seed,index)
.

For example:

rprng(4,2) = 17
rprng(4,5) = 21
rprng(4,2) = 17

Answer
int rprng(int seed, int index){
    srand(seed);
    while(index-- >0)
        rand();
    return rand();
}