Having srand/time issues... help?

This is a short piece of code I am writing. It's for a homework assignment but totally nothing to do with the homework assignment. I am trying to add flare to the project by adding fake system lag in the interface. I want the system to lag between half a second and 3 seconds on each line. I used define so I didn't have to type the whole line in between each step, but for some reason it always comes up with the same random time for delay... Any ideas?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 #include<iostream>
 #include<string>
 #include<iomanip>
 #include<fstream>
 #include<ctime>
 #define systemLag usleep(rand()%(3000000-500000) + 500000)
 #define systemSeed srand(time(NULL))
 using namespace std;

 int main()
 {   
     //define variables
     fstream cityFile;
     string cityChoice;
     string userName;
 
     //srand(time (NULL));
     //cout << systemLag;
 
     //menu
     cout << "This program written by Srand... good luck!\n";
     systemLag;
     cout << endl;
     systemSeed;
     systemLag;
     cout << "Logging into mainframe.....\n";
- call srand exactly once when your program starts. Never call it again (or at least... do not call it for each random number).

- Do not call rand() before you call srand()

- Do not use macros (unrelated... but macros are horrible. Break the habit!)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// a function instead of a yukky macro:
inline void systemLag()
{
    usleep( rand()%(3000000-500000) + 500000 );
}

 int main()
 {   
     // srand once and only once
     srand( time(nullptr) );

     //define variables
     fstream cityFile;
     string cityChoice;
     string userName;
  
     //menu
     cout << "This program written by Srand... good luck!\n";
     systemLag(); // <- call the function
     cout << endl;
 //    systemSeed; // <- get rid of this... only srand once!
     systemLag(); // <- call the function
     cout << "Logging into mainframe.....\n";
rand() generates an integer in the range between 0 and RAND_MAX. You should check the value of RAND_MAX on your system.
http://www.cplusplus.com/reference/cstdlib/rand/
http://www.cplusplus.com/reference/cstdlib/RAND_MAX/
@Disch, I appreciate the suggestion on macros. I have modified my code to use a function instead. I am surprised I hadn't thought of that! The point to the project is to use functions to call different options later in the program!

I am still having the same issue where the function always seem to pick the same random number, providing the same amount of emulated lag. I don't want that. Should I add the seed to the systemLag() function? I'll try that, but hopefully I am making sense...

Thanks for the responses!

@Chervil you make a great point sir! I might be trying to get a value outside of my system's max... I will check that out too.

You guys rock!
Problem resolved! Chervil, you were totally on track! I was past my RAND_MAX!!! So I modified the code as follows, using Disch's suggestion of using a function. That makes it SOOOO much easier to troubleshoot!

1
2
3
4
void systemLag()
 {
     usleep( rand()%((300-50) + 50)*10000);
 }


This was able to stay in my random generation as well as create the numbers necessary for the usleep command! Can I hug you guys?
Topic archived. No new replies allowed.