srand not working properly

519 views Asked by At

I'm writing a program that needs to fill two vectors with random ints(less than 1000) using 2 different seeded lists. when I try to use srand with the seeds I am supposed to use it gives me a very strange output.

here is the code I've written so far...

 #include <iostream>
 #include <vector>
 #include <algorithm>
 #include <cstdlib>
 #include <ctime>
 using namespace std;

 void Vectors(vector <int> v1, vector <int> v2, int s1, int s2) {
     srand(s1);
     for (int i = 0; i < 200; i++) {
        v1.push_back(rand () % 1000 + 1);
        //v1[i] = rand() % 1000 + 1;
     }
     srand(s2);
     for (int i = 0; i < 100; i++) {
         v2.push_back(rand() % 1000 + 1);
         //v2[i] = rand() % 1000 + 1;
    }
 }

 void prnt(vector<int> v) {
     for (int i = 0; i < 20; i++) {
        cout << v[i] << "   ";
         if (i == 9 || i == 19) {
            cout << endl;
         }
     }
 }

 int main() {
     vector<int> vec1;
     vector<int> vec2;
     vec1.resize(200);
     vec2.resize(100);
     Vectors(vec1, vec2, 1, 3);
     prnt(vec1);
     prnt(vec2);
     return 0;
     system("pause");
 }

however, when I run it the output I get is this...

0     29046     -309340552     29046     32     0     134113   0   0   0   
-309339528     29046     64     0   48   0   0   0   986   169   
0   0   -309340552   29046   32   0   134113   0   0   0   
-309339528   29046   64   0   48   0   0   0   986   169 

also, it will not allow me to use the Vectors method if vec1 and 2 are not initialized to some size.

I have just transferred from Java to C++ so any help you can offer me will be very appreciated as it is maddening to be stuck on something so trivial in Java

2

There are 2 answers

0
xaxxon On
void Vectors(vector <int> v1, vector <int> v2, int s1, int s2) {

You're passing in the vectors by value and not reference, so you're changing copies, not the originals.

Instead, pass them by reference:

void Vectors(vector <int> & v1, vector <int> & v2, int s1, int s2) {

and you'll be changing the original vectors and you'll see your changes outside the function.

0
Phil1970 On

Your output seems incorrect!

resize should set value to 0. Visual Studio 2017 does and as far as I know, this is expected behavior from the standard.

The above program should print 4 rows of five 0.

Vectors has no effect outside of itself has it make a copy and modify the copy (as already said in previous answer).

However, when you call prnt, the copy is only some efficiency wasting but you should print the same values as in main. Nevertheless, it is not a good idea to print 20 numbers without ensuring that you have them. It makes the code fragile... as one has to ensure that enough items have been added.

Having said that, your Vectors function is a poor design.

  • First, the name is meaningless.
  • Second, the code is repeatitive. So it would be much better to have a function that would randomize 1 vector from a given seed. Learn about DRY principle.
  • Third, it take a copy of vectors you it mainly only waste time.

Some side notes:

  • As already said by someone, you should use modern random generator too.
  • Either use reserve or update existing items instead of adding new one. It does not make sense to fill the vector with 200 zeroes and then add 200 extra random numbers at the end.