Random Number Generation Range Issues

Coding a guessing game with difficulties. The secret number can range anyone from 1 to 1000000, but when generating the number for anything over about 10000, I always get roughly the same number. Why is it the larger ranges all produce roughly the same number? ( Around 15,000 ).

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <iostream>
#include <fstream>
#include <windows.h>
#include <stdexcept>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


struct Difficulty_Struct {
    std::string name;
    int lower_limit;
    int upper_limit;
};

static const int MAX_DIFF = 6;
Difficulty_Struct Difficulties[MAX_DIFF];

int selected_difficulty;
int secret_number;

void load_from_file();
void game_initializer();
int get_input();
void difficulty_selection();
void generate_secret_number();






int main()
{
    game_initializer();
    difficulty_selection();
    generate_secret_number();
    return 0;
}



void load_from_file()
{
    std::ifstream file("GameData/Difficulty.txt");
    if ( file.is_open() )
    {
        int counter = 0;
        while ( file >> Difficulties[counter].name >> Difficulties[counter].lower_limit >> Difficulties[counter].upper_limit )
        {
            if ( counter >= MAX_DIFF - 1 )
            {
                break;
            }

            counter++;
        }

        file.close();
    }
    else
    {
        throw std::invalid_argument("Could not locate GameData/Difficulty.txt.");
    }
}

void game_initializer()
{
    srand (time(NULL));
    system("Color 1e");
    SetConsoleTitle("Number guess 1.0");
    std::cout << "Number Guess 1.0 by Max Daniels" << std::endl;
    std::cout << "-------------------------------" << std::endl;
    std::cout << "Please select a difficulty by typing the appropriate number!\n\n" << std::endl;
    load_from_file();
    for ( int x = 0; x < MAX_DIFF; x++ )
    {
        std::cout << (x + 1) << ") ";
        std::cout << Difficulties[x].name << " ";
        std::cout << Difficulties[x].lower_limit;
        std::cout << "-" << Difficulties[x].upper_limit << std::endl;
    }

}

int get_input()
{
    std::cout << std::endl;
    int choice;
    bool validInput = false;
    do {
        std::cout << ">";
        std::cin >> choice;

        if ( std::cin.fail() )
        {
            std::cin.clear();
            std::cin.ignore(100, '\n');
            std::cout << "Please input a valid number." << std::endl;
        }
        else
        {
            validInput = true;
        }
    }
    while ( !validInput );
    return choice;
}

void difficulty_selection()
{
    int choice = 0;
    enum {DifficultyOne = 1, DifficultyTwo, DifficultyThree, DifficultyFour, DifficultyFive, DifficultySix};
    while ( choice <= 0 || choice > 6 )
    {
        choice = get_input();

        switch ( choice )
        {
            case DifficultyOne: selected_difficulty = DifficultyOne - 1;
                break;
            case DifficultyTwo: selected_difficulty = DifficultyTwo - 1;
                break;
            case DifficultyThree: selected_difficulty = DifficultyThree - 1;
                break;
            case DifficultyFour: selected_difficulty = DifficultyFour - 1;
                break;
            case DifficultyFive: selected_difficulty = DifficultyFive - 1;
                break;
            case DifficultySix: selected_difficulty = DifficultySix - 1;
                break;
            default: std::cout << "Invalid selection, please select between 1-6." << std::endl;
                     choice = 0;
        }
    }
}

void generate_secret_number()
{
    secret_number = rand() % Difficulties[selected_difficulty].upper_limit + Difficulties[selected_difficulty].lower_limit;
    //Code for Testing
    std::cout << secret_number << std::endl;
}
The issue lies in the function on line 138. The random number generator is the first thing that starts in the program, line 69.
Is there an issue with using Rand() for large numbers?
yes I belive there is a problem with Rand and big numbers. If you have a compiler that can handle c++11 then you should use one of the new random number generators.

If you don't have c++11 then just use rand to generate three or four smaller numbers and multiply them together.
Topic archived. No new replies allowed.