Random Numbers

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);
}
 
O

osmium

DarthBob88 said:
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;

<snip>
You forgot to seed your random number generator. That is a FAQ.
 
D

DarthBob88

Just realized that. Tack in #include <time.h>, fiddle it to rand(time),
and it's all good. Thank you kindly, but I had difficulty finding the
FAQ. My apologies.
 
R

Rod Pemberton

DarthBob88 said:
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.

You're missing '#include <stdlib.h>'.

If you change this line:
printf("Hint: It's between %d and %d\n", low, high);

to this:
printf("Hint: It's between %d and %d %d\n", low, high, secret);

and run rand.exe, you'll see that this line:
int secret = rand() % 100 + 1;

is misplaced. It should be before the while().

rand() generates a pseudorandom number sequence. In other words, the
sequence is semi-random and is always identical for the same starting seed
value. If you need a different semi-random sequence, you'll need to call
srand() somewhere. Most documentation suggests you use time() for a random
value to use with srand().

srand(time(0));

There are a number of pseudo-random number generators which are
public-domain and developed by university professors (i.e., good quality)
for various purposes such as encryption or Monte Carlo simulations. Just
Google or Yahoo and do some reading to find better functions.


Rod Pemberton
 
R

Randy Howard

DarthBob88 wrote
(in article
Just realized that. Tack in #include <time.h>, fiddle it to rand(time),
and it's all good. Thank you kindly, but I had difficulty finding the
FAQ. My apologies.

You mihgt want to double check, it may APPEAR to be good now,
but using mod like you did in your original, the results may not
really be as random as they should be. The C FAQ is easy to
find. google for clc FAQ ... boom, #1 hit.

You might also want to google for mersenne twister.
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

DarthBob88 said:
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>
#include <stdlib.h>
#include said:
main () {
int guess, high, low;
high = 100;
low = 0;
char line[80];
srand(time(NULL));

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;
Uh, why a new number every time ?
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);
Check the return value of sscanf. always.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top