I am trying to predict the output of a program that uses msvcrt's rand() function for generating the face of three dice. I believe the code is something like:
dice[0] = rand() % 6 + 1; dice[1] = rand() % 6 + 1; dice[2] = rand() % 6 + 1;
, and I was wondering if I could use a prediction program for linear congruential generators to predict the next numbers in the sequence.
views:
228answers:
2
+3
A:
See for yourself: C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\rand.c
(Or use %VCINSTALLDIR%\crt\src\rand.c
if you're running from a VC command prompt.)
(Assuming you have at least the standard version of VC. It's two lines. I'd post it, but not sure whether the license allows it.)
Alex
2009-12-19 01:51:26
%VCINSTALLDIR%\crt\src\rand.c is more general.
codekaizen
2009-12-19 01:57:43
Indeed, thank you.
Alex
2009-12-19 02:08:10
Thanks, this is just what I needed. I went through all this just to find that the program I am trying to predict doesn't work as I predicted (it calls rand() in various other parts of the program, screwing up my seed).
Benjamin Manns
2009-12-22 17:34:56
+1
A:
If memory serves, yes it's a linear congruential generator -- but what it returns is the remainder of a larger output, which increases the difficulty of predicting the next number (to the point that you probably need a much larger sample than three outputs to do so dependably).
Jerry Coffin
2009-12-19 02:08:17