Thread: How To Get A Random Generated Output From A Coin Flip Program?

  1. #1
    Registered User
    Join Date
    Jul 2021
    Posts
    2

    How To Get A Random Generated Output From A Coin Flip Program?

    I have a homework assignment where I have to create a program that will flip a coin once and output "heads" / "tails" and afterwards prompts user if they want to flip again. If the user inputs "yes" program will flip coin than end, if user inputs "no" program will end.

    The issue I am running into is that the program only outputs the same side and does not give a random output.

    Code:
    #include <stdio.h>
    #include <stdlib.h> 
    
    
    
    char flip;
    
    
    void coinLoop()
    {
    for ( int i = 0; i < 1; i++ )
    {
    
    // loop start
    
    if (rand() % 2 == 0)
    {
    printf("Heads\n");
    
    
    } else 
    {
    printf("Tails\n");
    }
    
    
    }
    }  
    
    
    int main()
    {
    
    
    printf("\n\n"); //visual purposes
    
    
    coinLoop(); //function for first coin flip
    
    
    printf("\n\n"); //visual purposes 
    
    
    printf("Flip Coin? \n");
    scanf(" %c", &flip);
    
    
    if (flip == 'y') // will allow for coin to be flipped if user inputs yes 
    {
    printf("\n");
    
    
    coinLoop();
    
    } else if (flip == 'n')
    {
    return 0;
    }
    
    
    
    
    return 0;
    }
    Attached Images Attached Images How To Get A Random Generated Output From A Coin Flip Program?-screen-shot-2021-07-21-3-46-39-pm-jpg 

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    You need to call srand() once in the main function.

    Normally it is called like

    Code:
    srand(time(0));
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Jul 2021
    Posts
    2

    Issue Resolved!

    The fact that Ive been struggling with this for like 3 days, and it was just missing this!

    Thank you so much for your help, the program is giving random output now, I also had to include the C Library <time.h> to be able use srand(time(0));

    Code:
     
    
    #include <stdio.h>
    #include <stdlib.h> // allowed the use of rand() function
    #include <time.h>   // allowed the use of srand() function
                      
    
    
    char flip;
    
    
    void coinLoop()
    {
       for ( int i = 0; i < 1; i++ ) // flips coin once
        {
            
        // loop start
        if (rand() % 2 == 0)
        {
            printf("Coin Flip: *Heads*\n");
    
    
        } else 
        {
            printf("Coin Flip: *Tails*\n");
        }
    
    
        }
    }                  
    
    
    int main()
    {
    
    
        printf("\n\n"); //visual purposes
    
    
        srand(time(0)); // this function allowed me to get a random output everytime 
    
    
        coinLoop(); //function for first coin flip
    
    
        printf("\n\n"); //visual purposes 
    
    
         printf("Flip Coin Again? \n\n");
         scanf(" %c", &flip);
    
    
         if (flip == 'y' || flip == 'Y') 
         {
             printf("\n");
    
    
             coinLoop();
             
         } else if (flip == 'n' || flip == 'N')
         {
             printf("\n"); // visual purposes
    
    
             printf("*PROGRAM ENDED*\n\n");
    
    
             return 0;
         }
    
    
    
    
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Apr 2021
    Posts
    25
    By the way, the two output sides might be different, but they will always be the same every time the program is ran if you don't use srand. For example, in my case, your original code output Tails and then Heads for me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Coin Flip (console app) - Jumping into C++
    By Jasper Dunn in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2016, 06:07 AM
  2. Understanding Output Generated
    By Air in forum C Programming
    Replies: 10
    Last Post: 01-15-2009, 01:52 PM
  3. Reading output generated by php in C
    By nitinmhetre in forum C Programming
    Replies: 3
    Last Post: 01-20-2007, 07:05 AM
  4. Replies: 13
    Last Post: 12-21-2006, 05:28 PM
  5. i want my output to be generated
    By coolshyam in forum C Programming
    Replies: 3
    Last Post: 03-19-2005, 12:53 AM

Tags for this Thread