D
DarthBob88
Problem: Getting a random number, an integer. between 0 and 100. I've
tried rand() % 100 +1, but that hasn't been very random. Any
suggestions?
Story: I'm trying to write a simple guessing game. I think of a number
from 1 to 100, which you try to guess. So far, I've tried it a dozen
times and always gotten 84 as the number. The chances of that are
roughly 1 in 10^24. Something is up. Here follows my code.
#include <stdio.h>
main () {
int guess, high, low;
high = 100;
low = 0;
char line[80];
printf("I'll think of a number, a random number, between 0 and
100.\n");
printf("If you get it right, you get a prize. Come on, friend.");
while (1) {
int secret = rand() % 100 + 1;
printf("Enter your guess.\n");
printf("Hint: It's between %d and %d\n", low, high);
(void)fgets(line, sizeof(line), stdin);
(void)sscanf(line, "%d", &guess);
if (guess == secret) {
printf("Bingo!\n");
break;
}
if (guess > secret) {
high = guess;
printf("Sorry, too low.\n");
} else {
low = guess;
printf("Sorry, too high.\n");
}
}
return(0);
}
tried rand() % 100 +1, but that hasn't been very random. Any
suggestions?
Story: I'm trying to write a simple guessing game. I think of a number
from 1 to 100, which you try to guess. So far, I've tried it a dozen
times and always gotten 84 as the number. The chances of that are
roughly 1 in 10^24. Something is up. Here follows my code.
#include <stdio.h>
main () {
int guess, high, low;
high = 100;
low = 0;
char line[80];
printf("I'll think of a number, a random number, between 0 and
100.\n");
printf("If you get it right, you get a prize. Come on, friend.");
while (1) {
int secret = rand() % 100 + 1;
printf("Enter your guess.\n");
printf("Hint: It's between %d and %d\n", low, high);
(void)fgets(line, sizeof(line), stdin);
(void)sscanf(line, "%d", &guess);
if (guess == secret) {
printf("Bingo!\n");
break;
}
if (guess > secret) {
high = guess;
printf("Sorry, too low.\n");
} else {
low = guess;
printf("Sorry, too high.\n");
}
}
return(0);
}