0

I'm having some trouble with this code:

for (long long int loop = 0; loop < 50000000; loop++)
{
    srand( (unsigned)time(NULL) ); // set the seed
    int r = abs(rand()); // stores the random seed for this loop
    int res = loop + r - r * r / r; // random operations with the loop number and the random seed.
    cout << "Loop number: " << loop << ". " << "Result: ";
    cout << res << endl;
}//this was missing

If you run that code, you can see, very clearly in the console, that the output of it is only doing the calculations once every few seconds. What's going on? The number should be completely different for each loop, because it's doing calculations with random numbers. Instead, the number changes every x loops ran, and then it only seems to increase between these times it actually does the math.

Do I need to specify I want the loop to wait until everything is complete before moving on?

Gabriel
  • 3,564
  • 1
  • 27
  • 49
  • are you missing a } at the end? – Jimmy Dec 18 '14 at 11:58
  • Is that normal that you wrote `long long int` ? – Aleph0 Dec 18 '14 at 11:59
  • 2
    You should only call `srand` once in your program. At the moment you are resetting the seed every iteration, which if you do it multiple times in the same second (since `time` changes every second), you will get the same sequence of pseudo-random numbers of which you are only picking the first value. – Joseph Mansfield Dec 18 '14 at 11:59
  • 2
    @Aleph0, it's perfectly valid, it means the same as `long long` or `long long signed int` if you really like typing – Jonathan Wakely Dec 18 '14 at 12:00
  • 2
    @Aleph0: You can even mix the order, like `long signed int long` – Armen Tsirunyan Dec 18 '14 at 12:01
  • @JonathanWakely Ok I didn't know it was possible to write something like that in c++. – Aleph0 Dec 18 '14 at 12:02
  • yes, the long long int is intentional, and no, I'm no missing a }. –  Dec 18 '14 at 12:02
  • 1
    @Pyrobisqit: You *are* missing a } in your example, although I'm sure you have it in your actual code. I'd edit the question if I were you. – Armen Tsirunyan Dec 18 '14 at 12:03
  • @ArmenTsirunyan, the question has already been edited – Jonathan Wakely Dec 18 '14 at 12:03
  • Why are you calling `abs(rand())`? This code looks like it was written by taking a valid program and rearranging all the tokens randomly – Jonathan Wakely Dec 18 '14 at 12:04
  • @JonathanWakely because I don't really want the number to be negative, although it doesn't really matter that much. That abs() was added by myself, trying to avoid negative numbers to be outputted in the r variable. –  Dec 18 '14 at 12:18
  • @Pyrobisqit, but `rand()` never returns a negative number. – Jonathan Wakely Dec 18 '14 at 12:21
  • 1
    On a completely different point, why `r - r * r / r` (which is near enough 0, surely)? – doctorlove Dec 18 '14 at 12:25
  • @doctorlove I'm making a VERY BASIC (yes, i know I don't take advantage of SSE, AVX and the like) benchmarking application. The idea is to make the CPU go crazy making a defined amount of calculations. I don't care much about the result at all. Just want to set the CPU on fire. –  Dec 18 '14 at 12:30
  • @JonathanWakely You're right. Fixed. Thanks. –  Dec 18 '14 at 12:30
  • 1
    You'll need to try a bit harder than that, any decent compiler will eliminate redundant expressions like `r - r * r / r` – Jonathan Wakely Dec 18 '14 at 13:19
  • 1
    Use c++11 and random library. You can use mersenne twister or other random number generators with better entropy, faster and well, c++11 flavored. – Jose Palma Dec 19 '14 at 06:40
  • @Raistmaj I don't understand, what makes my code not C++11, and how do I use that library? –  Dec 19 '14 at 08:40

2 Answers2

12

Because you're doing srand in the loop with time seed. time()'s granularity is in seconds so until one second has passed it will return the same seed and therefore the same random number. Do srand outside the loop.

The point of seeding the rand function with srand is that the sequence of generated random numbers is different with each program run. You need only one srand in your program.

And by the way, rand() always returns a non-negative number, so abs is useless. Be careful though that r can be 0, and you do divide by r, which potentially has undefined behavior. do r = rand()+1 to be safe.

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • You're right. I was getting zeroes sometimes and wondering what was going on. Thanks! –  Dec 18 '14 at 12:27
1

Your seed is the same for the same second, so the random numbers with that seed will be the same. You could try taking out the srand.

srand( (unsigned)time(NULL) ); // set the seed
for (long long int loop = 0; loop < 50000000; loop++)
{
    int r = abs(rand()); // stores the random seed for this loop
    int res = loop + r - r * r / r; // random operations with the loop number and the random seed.
    cout << "Loop number: " << loop << ". " << "Result: ";
    cout << res << endl;
}

Cheers

Agus Arias
  • 467
  • 1
  • 7
  • 5