NO

Author Topic: simple random numbers  (Read 3653 times)

Hydrogen

  • Guest
simple random numbers
« on: October 04, 2010, 06:53:27 AM »
hi guys,

just began learning C a few days ago so i'm a real beginner. and my compiler is Pelles C.

i'm practicing newfound basics by creating a console based guessing number game. (don't laugh)

i've googled for various examples of random numbers in C and have got things like add stdlib.h and time.h, use randomize(); then the rand function etc, i've tried a few different ways, and pelles c compiles it fine, but went building gives errors of the use of randomize and rand etc.

I was just wondering if someone here can give me the quickest and easiest way to generate a random number for my app. it only needs to be between 1 - 10.

also, as a seperate question if I may (since I don't want to spam the forum with total newb topics), below is my current source code so far (it's small). and the players name is limited to 1 character of course, how do I write it so that the char variable can take a name and then display it in other functions such as "stats" and "game"?

any help would be greatly appriciated and I hope i've not violated any forum rules.

Code: [Select]
#include <stdio.h>

void singleplayer();
int stats(int r, int p, int c);
int game(int r, int p, int c);
char name;

main()
{
    int players;
    printf("\n\n--- Guess Number Game! ---\n\n");
    printf("is this game for 1 or 2 players? : ");
    scanf("%d", &players);
    if(players==1)
    {
        singleplayer();
    }
    if(players==2)
    {
       
    }
}

void singleplayer()
{
    int rounds=0;
    int playerscore=0;
    int computerscore=0;
    printf("\n\n1 player selected, you will play against the computer.\n\n");
    printf("please enter your name : ");
    scanf("\n%c", &name);
    stats(rounds, playerscore, computerscore);
    game(rounds, playerscore, computerscore);
}

int stats(int r, int p, int c)
{
    printf("\n\nCURRENT STATS:\n");
    printf("Rounds = %d", r);
    printf("\n%c = %d points", name, p);
    printf("\nComputer = %d points\n\n", c);
   
}

int game(int r, int p, int c)
{
    int playerselect;
    int computerguess;
    printf("%c, you are the selector. pick a number 1 - 10 : ", name);
    scanf("%d", &playerselect);
    printf("\n\nyou picked %d", playerselect);
}


Offline Robert

  • Member
  • *
  • Posts: 245
Re: simple random numbers
« Reply #1 on: October 04, 2010, 07:50:29 AM »
There is no randomize function in C unless you make one yourself. You can use, for example,

srand(GetTickCount()/1000.0);

Google srand and you'll find lots of examples.

Robert Wishlaw

Hydrogen

  • Guest
Re: simple random numbers
« Reply #2 on: October 04, 2010, 08:23:57 AM »
thank you Robert. i've checked it out and yes you are right, it is the srand function.

I used:

int generator()
{
   int number;
   /* initialize random seed: */
   srand ( time(NULL) );
   /*generate random number */
   number = rand() % 10 + 1;
   /* send the random number back to game into computerguess */
   return number;
}

i've also learned how to do the name in a variable by using a char array instead of just a char.

thanks alot!