Wednesday, May 28, 2014

Proper use of stand and rand() in c++

I had many issues with stand and rand when trying to generate truly random series of numbers.
I used in for or while loop and this should be written correctly.
Here is the correct way to write randomly generated functions in C++:

            srand(time(NULL));
            while ( j < N ) {
                std::array<int,Tsp::N> genes = {};
                rand();
                index = rand() % N;
                if ( std::find(std::begin(genes), std::end(genes), index) == std::end(genes) ) {
                    genes[j]=index;
                    j++;
                }
            }




No comments:

Post a Comment