0

I'm very new to programming and only have an extremely basic grasp of the C language. I was attempting to make a coin flip generator based on user input with my limited knowledge. I know there is something wrong with this code as it outputs the same values every time, I'm just not experienced enough to know how to fix it. Any help is appreciated, thanks!

Code:

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

int main() {
unsigned int userInput;
int heads, tails;
int randomNum;
heads = 0;
tails = 0;


printf("This program will calculate the possibilities of a selected number of coin flips.\n");

printf("Input what number of coin flips you want: \n");
scanf("%d", &userInput);
if(userInput == 0){
  printf("Input a value greater than 0.\n");
  exit(0);
}

while (userInput > 0){
randomNum = rand() % 2;
  if (randomNum == 1){
    heads = 1 + heads;
    userInput = userInput - 1;
  }
  else if (randomNum == 0){
    tails = 1 + tails;
    userInput = userInput - 1;
  }


}
printf ("There were %d heads flips and %d tails flips.\n", heads, tails);

  return 0;
}
Ala
  • 1
  • It seems you're running this code with the same seed every time. [Why do I always get the same sequence of random numbers with rand()?](https://stackoverflow.com/q/1108780/15497888) – Henry Ecker Nov 07 '21 at 04:18

0 Answers0