Not Randomizing Numbers

My program is repeating the same set of "random" numbers... I'm doing this is Xcode. Please help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <vector>
using namespace std;

//global constant
const int ROWS = 20;
const int COLS = 10;

//prototypes
double getAverage(vector <vector <int>> &);
int getLowest(vector<vector<int>>);

int main()
{
    //initialize random stuff
    unsigned time = 0;
    srand(time);
    
    //variables
    double average;
    
    //inititalize 2d vector
    vector <vector <int>> rowVector;
    vector <int> colVector;
    
    //fill 2d vector
    for(int counter = 0; counter < ROWS; counter++)
    {
        for(int index = 0; index < COLS; index++)
        {
            colVector.push_back((rand() % 91) + 30);
        }
        rowVector.push_back(colVector);
        colVector.clear();
        cout << endl;
    }
    
    //output vector
    for(int counter = 0; counter < ROWS; counter++)
    {
        for(int index = 0; index < COLS; index++)
        {
            cout << setw(3) << rowVector[counter][index] << " ";
        }
        cout << endl;
    }
    
    //getAverage
    average = getAverage(rowVector);
    cout << "The average is:  " << average << endl;
    
    //find highest
    
    return 0;
}

Last edited on
Line 29: You're calling srand() with an argument of 0 every time you run your program. i.e. You're using the same seed every time. Using the same seed is going to return the same sequence of numbers every time.

Call srtand() this way:
1
2
//  Delete line 19
  srand (time(0));


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You are seeding the random number generator with "time" which is an integer that is set to 0. If you seed the generator with the same number, you will always get the same sequence of results.

I think you wanted to call the time function and use that result to seed the RNG.
Lol god bless y'all. Problem solved.
Topic archived. No new replies allowed.