1

I have function which genrenate me random index like this :

size_t randomIndex(vector<int>myVector) {
    srand(time(NULL));
    size_t index1 = rand() % myVector.size();
    return index1; 
}

I call it inside another funtion :

void generaTwoIndex(vector<int> myVector2) {
     size_t index1 = randomIndex(myVector2); 
     cout << "index1 = " << index1 << endl; 
     size_t index2 = randomIndex(myVector2);
     cout << "index2 = " << index2 << endl;
}

It generate me the same index. What is the issue ? Thank's in advance for your answers.

  • ***What is the issue ?*** You are calling `srand(time(NULL));` in too soon which most likely causes the same seed (because the number of seconds did not change - although even if it did this would be wrong). My advice is to seed 1 time in your `main()`. – drescherjm May 23 '16 at 22:57
  • No ! it don't call it in the loop. In the seconde function I want to generate two different index randomly using the first function. – kerrache massipssa May 23 '16 at 23:02
  • ***No ! it don't call it in the loop.*** It is the same reason though. – drescherjm May 23 '16 at 23:02
  • It not the same think. In my case I dont use the loop http://stackoverflow.com/users/1413395/%cf%80%ce%ac%ce%bd%cf%84%ce%b1-%e1%bf%a5%ce%b5%e1%bf%96 – kerrache massipssa May 23 '16 at 23:04
  • 1
    ***It not the same think.*** It is. Think of your case as unrolling a loop of size 2. With that said read the answers below. They all explain the cause. – drescherjm May 23 '16 at 23:04

2 Answers2

7

time(NULL) returns the time in seconds since 1970. Since you call the function twice an a row, way less than a second passes between invocations, so time(NULL) returns the same value for both calls, and the RNG is seeded with the same value for both calls.

When a random number is generated, the generator is automatically seeded with its result. So you only need to seed it once:

size_t randomIndex(vector<int>myVector) {
    size_t index1 = rand() % myVector.size();
    return index1; 
}

void generaTwoIndex(vector<int> myVector2) {
    srand(time(NULL));    //I would recommend moving this line to main()
    size_t index1 = randomIndex(myVector2); 
    cout << "index1 = " << index1 << endl; 
    size_t index2 = randomIndex(myVector2);
    cout << "index2 = " << index2 << endl;
}
Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
FireAlarm
  • 92
  • 1
  • 6
1

srand() was created to make rand() deterministic, so what is probably happening is that because the two calls to srand() are called at almost the same time they get the same value for time(null), which determines future values of rand(). To fix this, change the second block to this:

void generaTwoIndex(vector<int> myVector2) {
     srand(time(NULL));
     size_t index1 = randomIndex(myVector2); 
     cout << "index1 = " << index1 << endl; 
     size_t index2 = randomIndex(myVector2);
     cout << "index2 = " << index2 << endl;
}

and remove srand(time(NULL)); from the first block.

Alastair Brown
  • 1,598
  • 8
  • 12