My rand() function does not work

My rand() function does not work I have fiddled about and it does not seem to want to work I an trying to make a guessing number program. If you could help me that would be great.

Thanks James

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

using namespace std;

int number(rand() % 11), Restart, inputnumber;


int main()
{


    while(inputnumber != number)
    {
       cout  << "\nIm thinking of a number between 1 and 10 guess it:";
    cin >> inputnumber;

    if(number == inputnumber)
    {
        cout << "\nYay you are psychic or you went through it logically\n";
    }


    else
    {
        cout << "\nWRONG\n ";
    }

    }


    return 0;
}
The issue is that you never call srand(), but really rand() is deprecated and you shouldn't use it. Use the random number generators in the <random> header:
http://www.cplusplus.com/reference/random/
http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution#Example
closed account (E0p9LyTq)
You are not seeding the random number generator (srand) before you call rand. You will always get the same pseudo-random number.

The C++ random number generators, while more complicated to use, give better ranges of random number distributions than the C library rand function.

http://www.cplusplus.com/reference/random/
Topic archived. No new replies allowed.