1

Programming in C, I am trying to run a game where the computer generates a number between 1 and 100. However, I keep getting 42 as the number generated... what is wrong with my "rand()"

#include <stdlib.h>
#include <stdio.h>
int main (void)
{
    int guess, tries = 6;
    int number = 1+(rand()%100);

    printf("Pick a number between 1 and 100:");
    scanf_s("%d", &guess);

    while(tries > 0 && guess != number)
    {
        if(guess > number)
            printf("Too high");
        else
            printf("Too low");
        puts(". Guess again:");
        scanf_s("%d",&guess);

        tries--;
    }        

    if(guess == number)
        puts("You win!");
    else
        puts("You lose");
}

after a good amount of discussion below I added in the fallowing lines of code

#include <time.h>

and

srand ((unsigned int)time(NULL));

but the code failed to run in MS visual studio until i googled the error and it turns out that MSVS needs the variebles to be declared at the top of each function/block. after doing this the code works fine. Thank you everyone and sorry for the multiple edits. corrected code:

#include <time.h>
int main (void)
{
int guess, tries = 6, number;
srand ((unsigned int)time(NULL));
number = 1+(rand()%100);

The rest is the same. Thank you all.

fubrick
  • 51
  • 6
  • How are you seeding your random number generator? – Hot Licks Sep 06 '13 at 18:14
  • 4
    (Though if you have to come up with a single answer, 42 is a good one.) – Hot Licks Sep 06 '13 at 18:15
  • Seed it first with [srand](http://www.cplusplus.com/reference/cstdlib/srand/). – E. Moffat Sep 06 '13 at 18:15
  • Check this one http://stackoverflow.com/questions/3297201/function-srand-in-c – zoska Sep 06 '13 at 18:16
  • 3
    Are you sure it's a bug? [You never can be sure](http://dilbert.com/strips/comic/2001-10-25/). [Maybe someone had a D42](http://xkcd.com/221/). – Joshua Taylor Sep 06 '13 at 18:16
  • Alternatively, consider to use `arc4random()` (if available on your platform). That does not need to be seeded. – Martin R Sep 06 '13 at 18:21
  • 1
    @user814064 if you keep on modifying your code, none of the answers are going to make any sense – P0W Sep 06 '13 at 18:41
  • 1
    Voted to close as “unclear what you're asking” since edit 3 dramatically changes the question. – Pascal Cuoq Sep 06 '13 at 18:42
  • @P0W: ??? I did not modify any code. I flagged it. – dcaswell Sep 06 '13 at 18:43
  • Maybe the author of your compiler was a fan of the [Hitchhiker's Guide to the Galaxy](http://www.amazon.com/Hitchhikers-Guide-Galaxy-Douglas-Adams/dp/0345391802)? – Mark Ransom Sep 06 '13 at 18:44
  • I don't understand the SO's programmers. Other days this topic could to have -3 downvotes instead of... – Jack Sep 06 '13 at 18:44
  • @user814064 sorry it was meant for OP, I didn't notice the userid – P0W Sep 06 '13 at 18:45
  • @user2755275 if you keep on modifying your code, none of the answers are going to make any sense – P0W Sep 06 '13 at 18:46
  • @P0W I edited the code to include the suggestions of 'user2755275' but i still get the missing ';' before 'type' error – fubrick Sep 06 '13 at 18:50
  • @PascalCuoq how is it unclear? i generated 42 every time. was asked by another user to edit the code and add in time.h and srand((unsigned int)time(NULL)); and I still get errors pertaining to missing ';' before 'type' – fubrick Sep 06 '13 at 18:56
  • 1
    @user2755275 you added `srand ( (unsigned int)time(NULL) );` and there's no question at all, read your title now. Does that even make sense ? If you have other doubt better start on another post, why ruining the existing one. FYI..typos aren't entertained much on SO. and the code works fine, I've already given you a link. – P0W Sep 06 '13 at 18:57
  • @P0W Ok, I edited my source back to original. I only edited it in the first place on request by 'ajp15243' ...Anyways I tried to run your version but i get a syntax error. – fubrick Sep 06 '13 at 19:04
  • @fubrick Well I assumed you would edit the rest of your question to in some way match the code edit... – ajp15243 Sep 06 '13 at 19:44

4 Answers4

7

Use a seed with time

To seeds the pseudo-random number generator used by rand() with the current time.

srand(time(NULL));

before calling rand

P0W
  • 46,614
  • 9
  • 72
  • 119
6

You need to initialize the random seed with srand (time(NULL));

In order to do so you also need to include time.h.

pzaenger
  • 11,381
  • 3
  • 45
  • 46
2

Try to use

srand ( (unsigned int)time(NULL) );

at the beginning of main.

Do not forget to Include

#include <ctime>

or

#include <time.h>

From the docs:-

In order to generate random-like numbers, srand is usually initialized to some distinctive value, like those related with the execution time. For example, the value returned by the function time (declared in header ) is different each second, which is distinctive enough for most randoming needs.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • thank you. it worked after i figured out that MS Visual Studio wants all variables declared at the top of their blocks. everything works now. – fubrick Sep 06 '13 at 19:25
0
#include <stdlib.h>
#include <stdio.h>

header file that has the srand(time(NULL)) function in it

#include <time.h>

.

int main (void)
{
    srand(time(NULL));

seeds the random generator

    int guess, tries = 6;
    int number = 1+(rand()%100);

    printf("Pick a number between 1 and 100:");
    scanf_s("%d", &guess);

    while(tries > 0 && guess != number)
    {
        if(guess > number)
            printf("Too high");
        else
            printf("Too low");
        puts(". Guess again:");
        scanf_s("%d",&guess);

        tries--;
    }        

    if(guess == number)
        puts("You win!");
    else
        puts("You lose");
}
Footsure
  • 91
  • 5