c - Is it possible to peek at the next rand value -
assuming generator has been seeded, possible peek @ next random value without changing it?
i.e. given:
#include <stdlib.h> int r; r = rand(); // 99 r = rand(); // 80 is possible
#include <stdlib.h> int r; r = peekatrand(); // give 99 r = rand(); // still gives 99 r = peekatrand(); // give 80 r = rand(); // still gives 80 furthermore, can extended peeking @ next n numbers?
you can implement peekatrand follows:
int peekatrand() { int r,s; s = rand(); srand(s); r = rand(); srand(s); return r; } to make "worthy", call srand((unsigned int)time(null)) @ beginning of program.
Comments
Post a Comment