Challenge #2, done? I also have a few questions!

+1 Jacky L · February 19, 2015
So here is my code:

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

    int main(void)
    {
        int i;
        int roll1;
    int roll2;
    int sum1 = 0;
    int sum2 = 0;
    char input;

    for( i = 0; i < 3; i++ ) {
        roll1 = ( rand()%6 ) + 1;
        printf( "Roll number #%d. ", i+1);
        printf( "You rolled a %d.\n", roll1);
        sum1 += roll1;
    }

    printf("Sum: %d\n", sum1);

    printf("Will your next dice roll sum be higher, lower, or the same as %d? (h/l/s)\n", sum1);
    scanf("%s", &input);

    i = 0;
    for( i = 0; i < 3; i++ ) {
        roll2 = ( rand()%6 ) + 1;
        printf( "Roll number #%d. ", i+1);
        printf( "You rolled a %d.\n", roll2);
        sum2 += roll2;
    }

    printf("New sum: %d\n", sum2);

    //higher
    if (sum2 > sum1){
        if(input == 'h'){
            printf("Lucky guess\n");
        }
        else{
            printf("You suck!\n");
        }
    }

    //lesser
    if (sum2 < sum1){
        if(input == 'l'){
            printf("Lucky guess!\n");
        }
        else{
            printf("You suck!\n");
        }
    }

    //same
    if (sum2 == sum1){
        if(input == 's'){
            printf("Lucky guess!\n");
        }
        else{
            printf("You suck!\n");
        }
    }

    return 0;
}


So I don't really get a random number each time my program is executed. I keep getting 6,6,5 as my first roll and then 6,5,6 for my second roll

Post a Reply

Replies

Oldest  Newest  Rating
+1 Dol Lod · February 19, 2015
This is a easy fix. Random actually means pseudorandom. Think of a set pattern of random integers. To get truly random numbers use srand() which actually makes random produce different results. Srand takes in a parameter of an integer. I use 0, but it should make no difference. Srand should be called before you call the rand() function. 
+1 Jacky L · February 19, 2015
So I don't really get a random number each time my program is executed. I keep getting 6,6,5 as my first roll and then 6,5,6 for my second roll. Why is this happening?
Also when am I supposed to use the '&' symbol? At first I didn't have it and the code wasn't working, and then I tried changing my 'char input' to 'char input[1]'. Why do these not work?
+1 Dol Lod · February 19, 2015
I told you, the rand function doesn't actually generate truly random numbers. It instead uses a sequence of pseudorandom numbers that will be consistent unless you call srand at the main function. Just put srand(0); before you ever call the rand function. 

You use the & symbol when you want to reference the address or change the value at the address. I will show how to use pointers. 

int a=5;
int*b=&a; // this creates a pointer b which points to the address of a 
*b=6; //this now changes the value at a's address to be 6. 

Scanf takes in two arguments, the type of data to read and the array to store the data that was read. 
Scanf's first argument is the type of data you will be reading in. The second argument is the address of the place you want to store the data at.

I hope this helps. 

I would additionally for safety, increase the size of your character array. Then manually set the second to last byte as the null byte to ensure it is identified as the end of a string. 

Lastly, apparently the fgets function works a lot better with getting string input so I would recommend using that instead. 
+1 c student · February 19, 2015
rand does not produce a truly random number.  srand does not create seeds for true randomness.
+1 Jacky L · February 20, 2015
This makes a lot more sense now. Thank you guys.
  • 1

C

107,188 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator