c - program using the rand () function -
i want make simple program in rand() function generates random number out of 1,2,3 , user asked predict number. if user predicts number correctly wins otherwise looses. here's program-
#include <stdio.h> #include <stdlib.h>  int main() {     int game;     int i;     int x;      printf("enter expected value(0,1,2)");     scanf("%d\n",&x);     for(i=0;i<1;i++){         game=(rand()%2) + 1          if(x==game){             printf("you win!");         }          else{             printf("you loose!");         }     } return 0;  } 
some issues code:
point 1:
    scanf("%d\n",&x); should be
    scanf("%d",&x); point 2:
for(i=0;i<1;i++) this loop practically useless. iterates one. either use longer counter, or rid of loop.
point 3:
it's better provide unique seed prng. may want use srand() , time(null) in function provide seed.
point 4:
game=(rand()%2) + 1 should be
game = rand() % 3; // ; maybe typo in case                 ^                 |           %3 generates either of (0,1,2) point 5:
when use % rand(), aware of modulo bias issue.
note:
- the recommended signature of main()int main(void).
- always initialize local variables. practice.
Comments
Post a Comment