same random number, how do I make them different?

My random seed keeps giving me the same value for both random numbers. How can I change my code so the random values aren't always equal to each other? When I have the user input the data the program runs fine but I can't get the program to solve itself because it always gets the answer on the first try.

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
 #include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

int randRange (int low, int high)
{
    srand ( time( NULL ) );
    return rand () % ( high - low + 1) + low;
}
int randguess (int low, int high)
{
    srand ( time( NULL ) );
    return rand () % ( high - low + 1) + low;
}


int main ()
{
    cout << "Please pick a number between 1 and 100: \n";
    int answer;
    answer = randRange ( 1, 100 );
    int guess = randguess ( 1, 100);
    int guess_low = 1;
    int guess_high = 100;

    for ( int i = 0; i < 10; i++ )
    {
        if ( guess < answer )
        {
            cout << "Your guess is too low: \n";
            cout << guess;
            guess_low = guess;
            guess = randRange (guess_low, guess_high);
        }
        if ( guess > answer )
        {
            cout << "Your guess is too high: \n";
            cout << guess;
            guess_high = guess;
            guess = randRange (guess_low, guess_high);
        }
        if ( guess == answer)
        {
            cout << "YAY! You got the answer!";
            cout << guess;
            break;
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <cstdlib>

int main(){
	std::srand(42);
	std::cout << std::rand() << ' ';
	std::cout << std::rand() << '\n';

	std::srand(42);
	std::cout << std::rand() << ' ';
	std::cout << std::rand() << '\n';
}
71876166 708592740
71876166 708592740
Thank you for the help. It seems that my error was that I was using srand() twice and that was causing it to use the same number when I needed to use srand once.
Topic archived. No new replies allowed.