Reputation: 79
my rand number is rand()%6+1 aka dice rolling, when its based on "time", is it possible to make a console app that foresees the future numbers in the time I want to? for example predict a number on time 14:40:32 on a certain day in future?
Upvotes: 1
Views: 2712
Reputation: 55594
Yes provided that you use the same implementation of rand
i.e. link with the same version of the standard library. All you need is to get the time_t
value for the time you are interested in pass it to srand
and call rand
to get the value.
For example, if time_t
holds the number of seconds since the epoch (which is the case for most implementations), then you can do the following to get the value returned by rand
with a 10-second-in-the-future seed:
std::srand(std::time(nullptr) + 10);
std::cout << std::rand();
(Leaving aside the questions of whether it's a good idea to use rand
at all.)
Upvotes: 1
Reputation: 57418
Yes and no. If you have a value of time_t
, then just run the same library version of srand()
on that value, and rand()
will definitely yield the same sequence.
But you need to be sure that
rand()
in both applications is the same, i.e., the number of calls between the srand() and the rand() you are interested in is known by you.The last point might be a showstopper.
Say that you know that the app was initialised with srand(T) and you know T. Now yes, you know all the future extractions of its rand(). But you still need to know at which point in the sequence you are.
The number extracted at 19:30:17 GMT will not depend on the '19:30:17 GMT', but on how many numbers have been extracted before since the call to srand().
TL;DR if you know the value that time(0) passed to srand(), you cannot predict the output of the rand() call at a given time. You can predict the output of the n-th call to rand() for any given n.
Upvotes: 0
Reputation: 1
... for example predict a number on time 14:40:32 on a certain day in future?
It's possible when knowing how exactly rand()
generates the pseudo random number on a certain seed (which is available for most compilers open source code implementation).
You have a certain seed number given from your date and time, thus you can just inspect the sequence of random numbers generated consecutively.
Upvotes: 0