A really simple guessing game in C

doretox · May 1, 2020

Here is the first of three basic C programs that I intend to post here as examples. In my early days I struggled to find real programs to exercise and see how concepts are applied. In this first program, I applied inicial concepts such as:

  • variables;
  • functions;
  • loops (ifs, while…).

I think the only part of my program that can confuse beginners is how I generated a random number here:

srand(time(0));
	int secretNumber = rand() % 100;

First of all, computers cannot generate true random numbers. They do this simply because computers follow a set of rules programmed by humans. To know more about random numbers, I strongly recommend you to visit random.org. This website has a lot of useful and interesting information about random numbers.

So what do we do to generate these numbers in small programs like mine? We can add a little trick using the current time. Every time we run our program the date will be different from the first time (even if it is for milliseconds).

You can read more about random numbers in C/C++ in this website.

If you’re a more experienced programmer feel completely free to change the code and open a Pull Request to make it easier and more beginner friendly.

Click here to access the source code or go to my github, or clicking on the icon in the footer of this website.

Twitter