about rand()

0 Gabriel Leong · July 31, 2015
This is Bucky second challenge

to guess if the next random total number is higher or lower then the previous roll
this is what i did but, even tho is rand(), it does seems to give same number every time, am i doing anything wrong?

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>

int main()
{
    int i;
    int dice1;
    int dice2;
    int dice3;
    int total1;
    int total2;
    char hl;

    for (i=0; i<=9; i++){
        dice1=(rand()%6) +1;
        dice2=(rand()%6) +1;
        dice3=(rand()%6) +1;
        total1=dice1+dice2+dice3;

        printf(" your total roll is : %d \n" ,total1);

    }
    printf("for the next roll, is total be high or low(press H/L) \n");
    scanf(" %s" ,&hl);

    dice1 =(rand()%6) + 1;
    dice2 =(rand()%6) + 1;
    dice3 =(rand()%6) + 1;
    total2=dice1+dice2+dice3;

    if(total1 > total2){
        if(hl== 'L'){

            printf("good job is %d", total2);
        }else{
        printf("u suck");
        }
    }else {
    if(hl== 'H'){
            printf("good job is %d", total2);
        }else{
        printf("u suck");
        }
    }

    return 0;
    }

Post a Reply

Replies

Oldest  Newest  Rating
0 Jon Z. · July 31, 2015
Include the time library.

#include <time.h>

and add the following line at the beginning of your main function, or anywhere before the rand function is called.

srand (time(NULL));

That should do the trick.
0 Gabriel Leong · August 4, 2015
thanks it worked XD
  • 1

C

107,203 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator