2

can any one help in this program?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void){
    srand(time(NULL));
    int r = rand();
    int op = 0;
    while(0==0){
        printf("Enter a value\n ");
        scanf("%i\n",&op);

        if (op == r ){
            printf("yes");
            break;  
        }

        else if( op<r)
            printf("Your guess is lower than the number");
        else
            printf("Your guess is higher than the number");
    }
    printf("%i",r);
    return 0;
}    

when i try the program in "terminal" the result is always : Your guess is lower than the number

i don't know what's going on? but when i use code blocks in windows it seems perfect.

is this problem from Linux or the compiler

i wrote cc main.c in terminal then " ./a.out"

Mohammad
  • 25
  • 4
  • 1
    Did you mean to limit the range of numbers your program can generate? As it is, `r` might be 11376, and you'd be guessing things like 56 and 87. – chris Jul 22 '12 at 15:29
  • Couple things to check: print out the value of `r` before starting to guess so you can see what the answer is, plus print out the value of `op` AFTER the scanf, to make sure your input is being processed properly. – Marc B Jul 22 '12 at 15:32
  • If you do want a range, keep in mind that `rand() % 100` is not the best way to go. Look into [this question](http://stackoverflow.com/questions/2509679/how-to-generate-a-random-number-from-within-a-range-c) to find out more. – chris Jul 22 '12 at 15:33
  • @chris - I don't think that this will be an issue here. It does not seem like the purpose of this code is to get a uniform distribution of random numbers. If this were a random walk for galaxy clustering or something then it might matter, but a simple guessing game will not likely be affected negatively by a non-uniform distribution of this negligible amount. – drjrm3 Jul 22 '12 at 15:36
  • @Laurbert515, Sorry if my wording was subtle, but I said keep it in mind. Any time it does matter to you, you'll know what you shouldn't use. Better to have the knowledge there, rather than actually use modulus operations when it does matter because you never knew what was wrong with it. – chris Jul 22 '12 at 15:38

4 Answers4

2

rand() will return a number that is up to RAND_MAX. This value is platform-dependent.

Using Visual C++ on Windows, RAND_MAX is equal to 32767. However, on Linux when using the GNU C Library, it will be equal to 2147483647. So you'll have a much larger range of numbers to guess from. This is probably why you always guess numbers that are too small on Linux but not on Windows.

Note that 2147483647 is the largest possible value for an int with gcc, so guessing higher than that will not give the correct result, because the guess won't fit in op.

As the others have said, you should limit the value of r to a platform-independent value, for example with rand() % 100.

interjay
  • 107,303
  • 21
  • 270
  • 254
  • I tried to print r in while loop, but when i try bigger than r , it's still did not show me " Your guess is higher than the number" – Mohammad Jul 22 '12 at 15:39
  • @mohammad, I think the problem is the `'\n'` in your `scanf` call. Every iteration seems to be one step behind with it there. – chris Jul 22 '12 at 15:42
1

It seems like your value of r can be quite large. I am not sure why it works better in Windows, did you try it many times? It could be that you got lucky and had it generate a number which was less than you guessed. I just ran it and placed in a printout and the value was r = 1262702361, which is quite large.

To keep the value in between, say, 1 and 100, use a mod operator. Something like r = r%100 will limit r to 1 - 100 range.

drjrm3
  • 4,474
  • 10
  • 53
  • 91
1

I think you should limit the range r can belong to, using something like:

 r = rand() % 100;//r will be between 0 and 99 
Razvan
  • 9,925
  • 6
  • 38
  • 51
0

rand returns a pseudo-random integer between 0 and RAND_MAX, but RAND_MAX can be a big number (2147483647 on my GNU/Linux). You should perform a modulo operation (% operator).

To make sure of this, you should print the value of r on the terminal.

md5
  • 23,373
  • 3
  • 44
  • 93