How would I implement the rand or the srand statement into this C Programming language. I have been reading about these 2 statements but I am a bit confused still. In need of assistance. I am a beginner in Programming....

#include <stdio.h>  //Main header
#include <stdlib.h>  //Standard header tells the compiler to insert the
                 //contents of stdlib in a praticular place.
#include <time.h>  //Time header



#define MAX_GUESSES 3
//Title of the game

int main(void) {
int i;  //loop variable
int guess;  //loop variable
int number = 13;  //winning number for the game
int correct = 0;  //0 = lose, 1 = win

printf("\nYou have 3 chances to guess a number from 1 to 20.\n");
//displays out on the computer screen


for(i =0; i < MAX_GUESSES; i = i + 1) {  //Beginning of loop
    printf("Enter a guess (1-20): ");  //Displays the output on the screen
   scanf("%d", &guess);  //used to read information that's written
if(guess == number) {
    printf("That's correct!\n");
    correct = 1;
    break;  //Command that breaks the loop
}
else{
    printf("Sorry, that's incorrect.\n");  //Shows the output if number guessed is wrong
}
}  //end of loop


if(correct == 1) {
printf("You win!\n");
//Displays this message if the user guesses the correct number
}
else {
printf("Game over. You lose. The number was %d.\n", number);
//Displays this message if the user guessed wrong 3 times
}

return 0;

}

Recommended Answers

All 4 Replies

including stdlib.h and time.h...

     srand (time(NULL));
     int iRand = rand() % 20 + 1;  //give me a random number between 1 and 20

More info can be found here:
http://www.cplusplus.com/reference/cstdlib/rand/

Note, however, that this method is only pseudo random, as noted in the link. Other options that can better support a uniform approach to random generation in C can also be found here:

http://stackoverflow.com/questions/822323/how-to-generate-a-random-number-in-c

note: this is not my code, but from the link above --

    /* Returns an integer in the range [0, n).
     *
     * Uses rand(), and so is affected-by/affects the same seed.
     */
    int randint(int n) {
      if ((n - 1) == RAND_MAX) {
        return rand();
      } else {
        // Chop off all of the values that would cause skew...
        long end = RAND_MAX / n; // truncate skew
        assert (end > 0L);
        end *= n;

        // ... and ignore results from rand() that fall above that limit.
        // (Worst case the loop condition should succeed 50% of the time,
        // so we can expect to bail out of this loop pretty quickly.)
        int r;
        while ((r = rand()) >= end);

        return r % n;
      }

}

The srand() function sets the random number generator "seed", which is used to generate random numbers with the rand() or rand_r(). The latter one uses the supplied seed value and is a weak generator. It is considered obsolete today. If you want a more "space age" random number generator, then use srand48() to seed the generator, and drand48() to get your random values. It uses what is call an LCG (linear congruential generator) with 48-bit math to get better distribution of your random values. Here is a link to a great tutorial on random number generators: http://www.phy.ornl.gov/csep/CSEP/RN/RN.html complements of the US Government and Oak Ridge National Laboratory - our tax dollars at work!

Here is the root page of those articles and tutorials: http://www.phy.ornl.gov/csep/
It is a really great bunch of tutorials and articles on computational physics.

For your simple program ...
the first suggest by Dani's @ryantroop is quite adequate.

include stdlib.h and time.h.

then ...

     /* seed the random gerator ... */
     srand (time(0) ); /* somewhere near the top of main */

     /* then can do this as needed */
     /* give me a random number between 1 and 20 */
     int iRand = rand() % 20 + 1; 
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.