# Thread: Only getting 0; random number generator

1. ## Only getting 0; random number generator

I keep getting 0 (I think) for my random number generator. I'm programming a guessing game where a number between 100 and 999 is generated and the user guesses the number.

Here is my generator code:

srand(time(NULL));
int N = 999;
int random = 100 + rand() % N;

2. try it like this...
Code:
`random = 100 + (rand() % N);`
Also if you are looking for a number betteen 100 and 999 your N value should be 899. (899 + 100 = 999)

3. try:
Code:
```srand(time(0));
int N = 999;
int random = rand() % N + 100;```

4. What leads you to "think" you are always getting zero results?

% operator is the same precedence as * or /, so the reordering or bracketing mentioned by Tater and Billy should not affect anything.

5. Originally Posted by grumpy
What leads you to "think" you are always getting zero results?

% operator is the same precedence as * or /, so the reordering or bracketing mentioned by Tater and Billy should not affect anything.
well, what makes me think I'm getting zero is that it's a guessing game . Whenever I input 000 as a guess, it gives me 3 correct prompts, one for the hundreds place, one for the tens place, and one for the ones place. any other number yields nothing.

And you are correct grumpy, Tater and Billy's corrections have not affected anything. But thanks for your attempt Tater and Billy

6. So... show us the rest of your code (in code tags please) lets see what's going on...

7. Originally Posted by scatterbrain
well, what makes me think I'm getting zero is that it's a guessing game . Whenever I input 000 as a guess, it gives me 3 correct prompts, one for the hundreds place, one for the tens place, and one for the ones place. any other number yields nothing.
Well .... unless you provide a small sample of code that repeatably exhibits the behaviour, you're forcing us to play a guessing game about what the problem is.

Sorry, but I have better things to do. Like clearing lint from my belly button.

8. Okay, I wanted to see if only minimal code was necessary for me to be helped before I dropped this on you guys...
Code:
```#include<stdio.h>
#include<time.h>
#include<math.h>

main(){

int hundreds, tens, ones, guess, guessnum, guesshundreds, guesstens, guessones;

/*number generator*/
srand(time(NULL));
int N = 999;
int random = 100 + rand() % N;

/*guess an 3-digit integer > allow for only 10 guesses */
for(guessnum=1; guessnum<=10; guessnum++){
scanf("%d", &guess);

guesshundreds=guess/100;
guesstens=(guess/10)%10;
guessones= guess%10;

/*if the guess equals the random generated number, game is over*/
if (guess==random)
printf("You guessed right. It is %d", random);

/*if you guess a correct number but not in the correct "place", you made a match */
if ((guess)== guesshundreds || (guess)==guesstens || (guess)==guessones)

/*if you guess the correct number in the hundreds place, you've made a hit */
if (guess== guesshundreds)
printf("%d is a hit!", guesshundreds);
/*if you guess the correct number in the tens place, you've made a hit */
if (guess== guesstens)
printf("%d is a hit!", guesstens);
/*if you guess the correct number in the ones place, you've made a hit */
if (guess== guessones)
printf("%d is a hit!", guessones);

}
return(0);
}```
So let's say it generates 556. So if someone guesses 335, they've made a hit but not a match (with the 5).

9. It looks like "mastermind" board game.

Anyway, you have two problems: you need to compare the guess with the random number, not the guess with parts of the guess; you need to compare individual digits, so except for guess==random, all the other comparisons involving a direct comparison with guess are wrong.

Consider guess = 123 and random = 145. guesshundreds would then be 1, but guess == guesshundreds would be false because 123 != 1.

You might find this easier to handle if you store the numbers as strings instead of as integers.

10. Finally solved the problem. Thanks laserlight, you were right. And thank you to everyone else... Now I'm not even sure if the number generator was malfunctioning before, but it works now.

Code:
```#include<stdio.h>
#include<time.h>
#include<math.h>

main(){

int hundreds, tens, ones, guess, guessnum, guesshundreds, guesstens, guessones, randomhundreds, randomtens, randomones;

/*number generator*/
srand(time(NULL));
int N = 999;
int random = 100 + rand() % N;

/*guess an 3-digit integer > allow for only 10 guesses */
for(guessnum=1; guessnum<=10; guessnum++){
scanf("%d", &guess);

guesshundreds=guess/100;
guesstens=(guess/10)%10;
guessones= guess%10;

randomhundreds=random/100;
randomtens=(random/10)%10;
randomones= random%10;

/*if the guess equals the random generated number, game is over*/
if (guess==random){
printf("You guessed right! It is %d\n", random);}

/*if you guess a correct number but not in the correct "place", you made a match */
else if ((guess)== randomhundreds || guess==randomtens || guess==randomones)

/*if you guess the correct number in the hundreds place, you've made a hit */
else if (guesshundreds== randomhundreds)
printf("%d is a hit!\n", guesshundreds);

/*if you guess the correct number in the tens place, you've made a hit */
else if (guesstens== randomtens)
printf("%d is a hit!\n", guesstens);

/*if you guess the correct number in the ones place, you've made a hit */
else if (guessones== randomones)
printf("%d is a hit!\n", guessones);

}
return(0);
}```

11. If you want random numbers between 100 and 999 then "N" should be 900, not 999, not 899. That will force the random value to be between 0 and 899 which, when added to 100, will produce a final value between 100 and 999.