C++ Random number guessing game

后端 未结 5 1529
我寻月下人不归
我寻月下人不归 2021-01-28 06:50

I have to write a program that will run a random guessing game. The game is to be numbers from 1-100, the guesser gets 20 tries and at the end is supposed to be asked if they wo

5条回答
  •  灰色年华
    2021-01-28 07:09

    May I suggest using the gettimeofday function, as a way to provide a seed to your random function?

    #include 
    #include 
    #include 
    //clock_t times(struct tms *buffer);
    int
    main(int argc, char **argv)
    {
        int a;
        struct timeval _wall;
        gettimeofday( &_wall, NULL ); //(_wall.tv_sec,_wall.tv_usec);
        srand(_wall.tv_usec);
        a = rand() % 100 + 1;
        printf("%d\n",a);
        return 0;
    }
    

提交回复
热议问题