random number function not working

I wonder if anyone can help... I have done some debugging, and it looks like my random function either isnt being called each time as expected or isnt producing random numbers - just same numbers each time?

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  #include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>

std::vector<int> GetNumbers();

std::vector< std::vector<int> > StoreGetNumbersInVector();


int main(int argc, const char * argv[]) {
    
    std::vector< std::vector<int> > StoredNumbers = StoreGetNumbersInVector();
    
    
    for(const auto &line : StoredNumbers){
        for(const auto &numbers : line){
            std::cout << numbers << " ";
        }
        std::cout << std::endl;
    }
    

    return 0;
}


std::vector<int> GetNumbers(){
    
    int numberofballs = 60; // 60 to account for off by 1 error
    int line = 0; //number of balls in a line set to one and inc to 6
    
    
    std::vector<int> lotterynumbers(numberofballs, 0);
    std::vector<int> LottoLine;
    
    srand(static_cast<unsigned int>(time(NULL))); // randomise seed
    
    //generate 6 random numbers and set that element to the random number
    while(line < 6) {
        int RandomNum = 1 + rand() % 59;
        if (lotterynumbers[RandomNum] == 0){
            lotterynumbers[RandomNum] = RandomNum;
            ++line; //increment for stop condition of loop
        }
    }
    
    for( const auto numbers : lotterynumbers){
        if( numbers != 0){
            LottoLine.push_back(numbers);
        }
    }
    
    return LottoLine;
    
}

std::vector< std::vector<int> > StoreGetNumbersInVector(){
    
    std::vector< std::vector<int> > VectorOfVectors;
    
    std::vector<int> temp;
    
    for(int i = 0; i < 6; i++){
        temp = GetNumbers();
        VectorOfVectors.push_back(temp);
    }

    std::cout << std::endl;
    
    return VectorOfVectors;
}

By putting srand in GetNumbers you are calling it for each line and the time update won't be sufficient to change the seed. Effectively, you start from the same point in the random-number sequence for each line.

Move the call to srand to where it will be called only once; e.g the start of main.
Last edited on
I see you are calling the function GetNumbers() repeatedly in a loop.

That would be ok, but for one problem. The function GetNumbers() itself calls srand() using the current time as a seed. If (as is almost a certainty) the loop executes entirely within the same second of time, the seed will be identical each time (since the time is still at the same second) and that resets the number generator to repeat the same sequence.

What you should do is move the srand() call from the loop. Call it just once at the start of main() .
Cheers!! I can't believe it was so easy! Been scratching me head for ages over this...
Topic archived. No new replies allowed.