Thread: Getting a random number between 1 and 6

  1. #16
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    to check for every random number to see if they are a 3 or a 5?
    "I tend to use my inbox as a todo list. But that's like using a screwdriver to open bottles; what one really wants is a bottle opener" - Paul Graham

  2. #17
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    What do you hope to have happen here?

    Code:
    ...
    
    int i(int m, int n)
    {
    	return (rand() % (n-m) + m);
    }
    
    ...
    
    int main()
    {
        ...
    
        if (i==5){
            cinco++;
        }
    
        ...
    
    }


    This is not how you call a function, get rid of the parts in red.

    Code:
    if (w==1){
        int newgame(void);
    }

    At this point in the code, you have not properly initialized suma1.
    Code:
    if (s1==suma1){
        printf("\n*YES*\n");
    }
    To actually have your code go back to the beginning, you will need some type of looping construct, i.e. while,for,do/while... Currently if you select to go again, you will simply clear the screen and then exit the program.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #18
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    Yeah, I have some stuff that shouldn't be there yet, but I thought that wouldn't affect the program.
    Thanks for your help
    Alastor

    edit: i==5 checks if the first random number is 5 (I hope I got that right...). When it's finished, it has to check for 3s and 5s. So the variable cinco goes up by one and before 'printing' the result multiplies cinco by 5 and the tres by 3 and adds it up.
    Last edited by Alastor; 10-19-2005 at 06:35 PM. Reason: pizza
    "I tend to use my inbox as a todo list. But that's like using a screwdriver to open bottles; what one really wants is a bottle opener" - Paul Graham

  4. #19
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > i==5 checks if the first random number is 5 (I hope I got that right...)
    Wrong - it compares the address of the function with 5
    Man, if you ever bothered to turn on warnings in your compiler you'd see what a mess it was.

    i is a function pointer
    i(1,6) is a function call.

    So you're comparing a pointer to an integer - hence the first warning.

    Code:
    $ gcc -W -Wall -ansi -pedantic -O2 foo.c
    foo.c: In function `main':
    foo.c:72: warning: comparison between pointer and integer
    foo.c:58: warning: unused variable `a'
    foo.c:58: warning: unused variable `m'
    foo.c:58: warning: unused variable `n'
    foo.c:58: warning: unused variable `tres'
    foo.c:58: warning: unused variable `suma2'
    foo.c:58: warning: unused variable `s2'
    foo.c:91: warning: control reaches end of non-void function
    foo.c:58: warning: 'suma1' might be used uninitialized in this function
    Now start at the beginning with much smaller bits of code and make sure you understand what's going on before you spool out a whole mess and then try and fix it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #20
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    I only got one warning, the warning you mentioned
    "I tend to use my inbox as a todo list. But that's like using a screwdriver to open bottles; what one really wants is a bottle opener" - Paul Graham

  6. #21
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    Ok, I fixed the warning thing, but the program is still not getting the right amount of petals...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    int newgame(void);
    
    int i(int m, int n)
    {
    	return (rand() % (n-m) + m);
    }
    
    int i2(int m, int n)
    {
    	return (rand() % (n-m) + m);
    }
    
    int i3(int m, int n)
    {
    	return (rand() % (n-m) + m);
    }
    
    int i4(int m, int n)
    {
    	return (rand() % (n-m) + m);
    }
    
    int i5(int m, int n)
    {
    	return (rand() % (n-m) + m);
    }
    
    int main()
    {
    	int a,w,cinco,c,t,tres,suma1,s1;
    
    	suma1=c+t;
        cinco=0;
        tres=0;
        c=4*cinco;
        t=2*tres;
    
        srand( (unsigned)time( NULL ) );
    
    	puts("\n\t############################\n"
    		 "\t###Petals around the rose###\n"
    		 "\t############################\n");
     	printf("The computer will roll five dice and ask you to guess the score for\n"
    		   "the roll. The score will always be zero or an even number. Your mission is\n"
    		   "to work out how the computer calculates the score\n");
    	getchar();
    	printf("%d %d %d %d %d", i(1,6),i2(1,6),i3(1,6),i4(1,6),i5(1,6));
          if (i(1,6)==5){
        cinco++;
       	}
        if (i2(1,6)==5){
        cinco++;	
        }
        if (i3(1,6)==5){
        cinco++;
       	}
        if (i4(1,6)==5){
        cinco++;
       	}
        if (i5(1,6)==5){
        cinco++;
        }
        if (i(1,6)==3){
        tres++;
       	}
        if (i2(1,6)==3){
        tres++;
       	}
        if (i3(1,6)==3){
        tres++;
       	}
        if (i4(1,6)==3){
        tres++;
       	}
       	if (i5(1,6)==3){
        tres++;
       	}
            /*Aca hay que agregar if's para checquar todos los numeros, si son 3ces o 5cos */
        scanf("%d", &s1);
    	if (s1==suma1){
    		printf("\n*YES*\n");
    	    printf("\nTo try again type 1\n");
    	    scanf("%d", &w);
            if (w==1){
    		newgame();
             }
        }
        else {
    		printf("\nSorry, the rose has %d petals\n"
    			   "\nTo try again type 1\n", suma1);
            scanf("%d", &w);
            if (w==1){
    		newgame();
            }
    		else{
    			printf("\nThanks for playing\n");
    		}
    	}
     }
    
    
    
    
    int newgame(void)
    {
      system ("cls"); /* Or system ("clear"); for Unix */
      getchar;
      main();
      return(0);
    }
    "I tend to use my inbox as a todo list. But that's like using a screwdriver to open bottles; what one really wants is a bottle opener" - Paul Graham

  7. #22
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    Returns a DIFFERENT number each time you call it - that's what rand is all about.

    printf("%d %d %d %d %d", i(1,6),i2(1,6),i3(1,6),i4(1,6),i5(1,6));
    if (i(1,6)==5){
    Which means your 2nd call to i() will likely be a different answer, so even if you get a 5 printed, there's not much chance that your if() statement will see a 5.

    > foo.c:58: warning: 'suma1' might be used uninitialized in this function
    You didn't fix this.
    int a,w,cinco,c,t,tres,suma1,s1;
    suma1=c+t;
    You're still setting suma1 based on uninitialised values of c and t
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #23
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    Code:
        c=4*cinco;
        t=2*tres;
    "I tend to use my inbox as a todo list. But that's like using a screwdriver to open bottles; what one really wants is a bottle opener" - Paul Graham

  9. #24
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    Wake up

    Code does not travel into the future to find out what a variable is going to be, then compute the result.

    You can only BEGIN to do this calculation AFTER the final tres++; in your code
    And then it needs to be
    Code:
        c=4*cinco;
        t=2*tres;
        suma1=c+t;
        if (s1==suma1)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #25
    Registered User
    Join Date
    Oct 2005
    Posts
    5
    ^^ lol..

    yeah, since they are all the same function... just call it again and again, thats what functions are for.. lol

Popular pages Recent additions subscribe to a feed