Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7655325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:29:13+00:00 2026-05-31T12:29:13+00:00

I am writing a simple C program that tosses a coin 100000 times and

  • 0

I am writing a simple C program that tosses a coin 100000 times and calculates how many heads and how many tails resulted from the toss using srand and rand. Here is what I have:

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

int main(void){

    int i;
    int h=0;
    int t=0;
    int n;

    for(i=0;i<100000;i++){
        srand(time(NULL));
        n=rand()%2;
        if(n==0){
            h++;
        }
        else {   
            t++;
        }
    }

    printf("%d heads\n", h);
    printf("%d tails\n", t);

    return EXIT_SUCCESS;
}

However when running the program I sometimes get 100000 heads and 0 tails and sometimes 0 heads and 100000 tails and sometimes I get random numbers in heads and tails but not in any way a 50% heads 50% tails.

Am I missing something?
Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:29:14+00:00Added an answer on May 31, 2026 at 12:29 pm

    You are seeding the pseudo-random number generator with the same seed each time around the look. Simply move the call to srand outside the loop.

    srand(time(NULL));
    for(i=0;i<100000;i++){
        n=rand()%2;
        .....
    

    A pseudo-random number generator (PRNG) is deterministic. That is, given the same initial seed, it will return the same sequence of numbers each time is is called. In your code, since you seed every time you call rand, you get the same sequence of 1 number for every iteration of your loop. Now, different executions of the program will have different results, because time will have changed.

    And in the cases when you were not seeing 100% heads or 100% tails, that would be when the loop took sufficiently long for time to have advanced.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a simple little ocaml program that reads an algebraic statement in from
I am writing a very simple program that removes duplicate chars from a string.
I'm writing a program that calculates input a user gives, simple numbers. 2, 4,
I am writing a simple program that calculates the cost of ice cream. The
I am writing a simple shell program that must take input from a file
I'm writing a simple program that will run entirely client-side. (Desktop programming? do people
I am writing a simple chat program that shows different dialogs in different tabs
I am writing a simple program to filter out stock quotes from a given
I'm writing a simple program to call a Java function from my C program.
I just started learning Python... I am writing a simple program that will take

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.