0

I'm trying to write a simple function that generates an array with random integers.

I encountered with an interesting thing, when I run this program outputs -each cout statements- seem the same.

However, when I am debugging and watching it step by step array values are changing.

Am I doing something wrong?

Thanks.

void generateRandomArray( int *&inputArray, int size){

    if(inputArray != NULL){
        delete []inputArray;

    }

    //create a new array
    inputArray = new int [size];

    //fill with random numbers   
    srand(unsigned (time (NULL)));
    for (int i = 0; i < size ; i++)     
        inputArray[i] = (rand() % 100) ;

}
int main(){

    //Variables
    int *inputArray = NULL;
    int size;


    //Test
    size = 10;

    //first call
    generateRandomArray( inputArray, size);

    for(int i = 0 ; i < size; i++){
        cout << inputArray[i] << endl;
    }

    cout << "------------------" << endl;

    //second call
    generateRandomArray( inputArray, size);

    //output is the same with previous
    for(int i = 0 ; i < size; i++){
        cout << inputArray[i] << endl;
    }
    return 0;
}    
selpak
  • 17
  • 6
  • 7
    Call `srand` once. – chris Jul 11 '14 at 21:31
  • 2
    Call `srand(time(NULL));` just after you start main. – Martin York Jul 11 '14 at 21:31
  • 1
    Don't use that legacy C stuff. http://en.cppreference.com/w/cpp/numeric/random – n0rd Jul 11 '14 at 21:32
  • Once it works. Goto [Code Review](http://codereview.stackexchange.com/) to get input on better techniques. – Martin York Jul 11 '14 at 21:34
  • 1
    If you single-step through the code really fast while debugging, you'll observe the same result as without the debugger attached :) – Praetorian Jul 11 '14 at 21:35
  • 1
    Read about the new [pseudo-random classes](http://en.cppreference.com/w/cpp/numeric/random) and [`std::generate`](http://en.cppreference.com/w/cpp/algorithm/generate) and [`std::generate_n`](http://en.cppreference.com/w/cpp/algorithm/generate). – Some programmer dude Jul 11 '14 at 21:35
  • 1
    @sp2danny: The parameter is a reference to a pointer. (Types are read right to left). A pointer to a reference is illegal and will not compile. – Martin York Jul 11 '14 at 21:35
  • BTW, use `std::vector` instead of raw array. – Jarod42 Jul 11 '14 at 21:37
  • but what's the reason of the difference btw debugging output and actual output? – selpak Jul 11 '14 at 21:38
  • the time it takes, srand the next second will produce a new value – sp2danny Jul 11 '14 at 21:39
  • 1
    while not debugging both calls to `time(NULL)` happen within one timer tick, hence same sequence is generated for every call. While debugging, some time passes between calls, so different sequence is generated. – n0rd Jul 11 '14 at 21:40

4 Answers4

0

The program runs too fast, srand(unsigned (time (NULL))); will get a seed having the same seconds (since epoch). Hence, the rand sequences are the same.

0

The problem is that if the calls to srand all happen withing the same second, the generator will be seeded with the same values and so generate the same sequence, always.

When you're debugging it will be slower, and allow time to pass and have different seed for the generator. When not debugging the time between calls will be too fast.

Don't call srand more than once, unless you know what you're doing. Put it at the start of the main function, and don't call it again.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

Call srand(time(NULL)) and it will seed the current time and make a random number from it. Since time is changing all the time, it will give you a different number every time.

Hoang Minh
  • 1,066
  • 2
  • 21
  • 40
0

With C++11, you may use something like: (https://ideone.com/EIb0nY)

#include <iostream>
#include <random>
#include <vector>

std::vector<int> generateRandomArray(std::default_random_engine& generator, int size)
{
    std::uniform_int_distribution<int> distribution(1, 100);
    std::vector<int> res(size);

    for (auto& e : res) {
        e = distribution(generator);
    }
    return res;
}

int main()
{
    std::default_random_engine generator;
    const int size = 10;

    //first call
    for (auto e : generateRandomArray(generator, size)) {
        std::cout << e << std::endl;
    }

    std::cout << "------------------" << std::endl;

    //second call
    for (auto e : generateRandomArray(generator2, size)) {
        std::cout << e << std::endl;
    }
    return 0;
}

That use the new random facilities.
It avoids manual memory management (with std::vector).

Jarod42
  • 203,559
  • 14
  • 181
  • 302