2

C++ question here, using Code::Blocks. I'm trying to run this code to test the pseudo-random function

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    int count = 0;
    while (count < 10){
        srand(time(NULL));
        cout << rand() << ' ';
        cout << (time( NULL )) << " \n";
        count++;
    }
    return 0;
}

The output from this is 10 equal lines. This is not really the problem, as the seed here is the same, so the result should be the same. The problem is that if I run this program again it gives 10 very similar lines with little variation not only on the time() output, but on the rand output too.

The srand(time(NULL)) is giving very similar answers that are basically the same return value, only a little bigger.

(Returning 9631 on first run, and then 9656 on the second).

My question is, is that the expected behavior? And how can I get more different results like 38 on the first run, and 671 on the second?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Jim_Maru
  • 23
  • 2

3 Answers3

2

Lots of misconceptions here... That the difference between two time(NULL) calls close to each other is small, is expected. After all, time moves only so fast. The next problem is that rand() returns a (pseudo) random value (of varying quality): Random in this case means that you may get 33 repeatedly a few times, as long as it's not predictable. That being said, rand() is implementation dependent, and it may very well be that your implementation uses something like an LCG, which doesn't generate good uniform random values. The only fix for that, is switching to a different rng. Since this is tagged as C++, you may want to take a look into C++11s random header and use something like their mersenne twister implementation, which is a good pseudo random number generator which produces random numbers with great quality and uniform distribution.

JustSid
  • 25,168
  • 7
  • 79
  • 97
  • I know all this. All I'm saying is that, once I know the value I get the first time I ran my program, I can expect what value I get if I close it and run it again. I can even time how long it takes for that number to reach a certain point and then I can always predict the number I will get from it. It's not giving me anything random. Not even anything pseudo-random. If I just printed "Time(null)" the result would be piratically the same: An always increasing number that grows at a certain speed. – Jim_Maru Dec 21 '13 at 08:56
1

The difference between varying executions will presumably be a small difference of change in time. The results of rand can be different for different C runtimes, but here is the implementation of rand from Visual Studio 10.

int __cdecl rand ()
{
    _ptiddata ptd = _getptd();

    return( ((ptd->_holdrand = ptd->_holdrand * 214013L
        + 2531011L) >> 16) & 0x7fff );
}

Where holdrand stores the seed to start to start of with. This is a linear congruential generator which typically don't produce high quality randomness. It's also throwing away a lot of state each time which doesn't help.

Casey
  • 41,449
  • 7
  • 95
  • 125
Steve
  • 7,171
  • 2
  • 30
  • 52
  • So basically, that means that the way rand works is, in fact, a linear and predictable function? Well, then I guess the only way to make it seem seem like it return a random value is if I limit the max value I can get from it. Like using rand(time(0))%10 or % a single digit number. The bigger the result I try to get the more similar and predictable the results will be. – Jim_Maru Dec 21 '13 at 23:47
0

To make a random number with a nearly the same seed(time), you can add a static variable to make rand() behaves different even with same parameter; or, you can make the parameter change when you get same time. For example:

int t=0;
...
rand(t=(t*7)^time(NULL));
SliceSort
  • 357
  • 3
  • 5
  • That's actually a great solution! I did that and I could get 100 different solutions using the same time (by running this program once, with the while count going up to 100). Running it again it seems like the predictability of the solutions is also very, very, low. Only thing I have to add is that the function call that I needed to change was srand, not rand. But I got what you meant. Thanks a lot! – Jim_Maru Dec 21 '13 at 23:56