1

I am writing a program that uses random numbers extensively in different ways and I am getting the same random numbers each time. I know to put srand(time(NULL));at the beginning of the program to seed the random number generator and so I have that, but it isn't working. Perhaps it has something to do with XCode or the Mac or something else? I can't find a similar problem online that I has a solution I haven't already tried. Some ways I'm using the random numbers are:

for (int i=0; i<num; i++)
{
    chrom_arr[i] = i;
}
random_shuffle(&chrom_arr[0], &chrom_arr[num-1]);

(to get an array with a series of random ints between 0 and num-1)

int crossover = rand() % num;

and other simple things like that. Even though I have srand(time(NULL)); at the beginning, it still doesn't work. I've also tried srand(time(0)); and also putting it in different parts of the program, but I have since learned that's not right.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Alex
  • 15
  • 1
  • 3
  • 1
    Actually that will be between 0 and num-2, as the end iterator should be one past the end. Using `std::end` would probably have prevented that. – Neil Kirk Sep 09 '15 at 20:59
  • You always get the same numbers? Even if you wait five minutes between runs? – rici Sep 09 '15 at 21:09
  • You're right - that is true and I just fixed that. I'm still getting the same random numbers though (however now they're different from the ones I kept getting before) – Alex Sep 09 '15 at 21:10
  • Call `srand` exactly once, before any calls to `rand`, and call it with `time(NULL)` as the parameter. – David Schwartz Sep 09 '15 at 23:31
  • David- I did do that, except now I have accidentally erased my whole program so I have to do the whole thing over. Who knows, maybe it won't do this weird thing anymore :/ – Alex Sep 10 '15 at 00:11

2 Answers2

2

Alex, can you please post a small but complete program that fails to generate different random numbers every time you run it? I'd be interested to see it...

Here's one that (of course) does yield different numbers every time it is run on my Mac:

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

int main()
{
    srand(time(NULL));
    for (int i = 0; i < 10; ++i)
        std::cout << rand() % 10 << std::endl;
    return 0;
}

What does it do if you compile and run it several times on your computer?

UPDATE:

I thought you meant srand() + rand() also produced identical numbers every time. That is not true. However, you are right about the behaviour of srand() + random_shuffle(): it may indeed produce the same numbers every time, depending on your compiler. It does on my compiler (clang on Mac) too.

This is explained here.

Community
  • 1
  • 1
WhiteViking
  • 3,146
  • 1
  • 19
  • 22
  • You may not believe this, but in opening up an Xcode project to run this code, I accidentally deleted my whole project which I had been working on for over a week. I kid you not. So now I don't even know anymore. I have to start all over. I swear I'm not kidding. – Alex Sep 09 '15 at 22:30
  • WhiteViking - I did run this code, and it did create different random numbers each time. The code which I've now rewritten (not the whole program) is still giving the same random numbers each time. I'm pretty sure it's happening here: random_shuffle(&chrom_array[0], &chrom_array[num-1]); – Alex Sep 10 '15 at 02:01
1

You should try somethig like this:

srand(static_cast<unsigned int>(time(0)));
std::shuffle(chrom_arr.begin(), chrom_arr.end(), default_random_engine(rand()));

This will work as long as chrom_arr is a std::vector. Once you are using C++, I presume this is what you are trying to do.

Wagner Patriota
  • 5,494
  • 26
  • 49