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

  • Home
  • SEARCH
  • 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 3597330
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:06:15+00:00 2026-05-18T20:06:15+00:00

I was creating a simple program that simulates a coin toss for my class.

  • 0

I was creating a simple program that simulates a coin toss for my class. (Actually, class is over this term and i’m just working through the rest of the projects that weren’t required). It involves the creating and calling a function that generates a random number between 1 and 2. Originally, I tried to seed the random number generator within the function that would be using it (coinToss); however, it did not produce a random number. Each time the program was run it was the same number as though I had only used

rand()

instead of

unsigned seed = time(0);
srand(seed);
rand();

Yet, when i moved the above within

int main()

it worked fine.

My question is 1)why did it not work when setup within the function that called it and (2) how does rand()
have access to what was done by srand() if they do not both occur in the same function?
Obviously, i’m a beginner so please forgive me if i didn’t formulate the question correctly. Also, my book has only briefly touched on rand() and srand() so that’s all i really know.
thanks for any help!

Pertinent code:

First attempt that didn’t work:

int main()
{  
    //...........
    coinToss();
    //...........
 }

 int coinToss()
 {
    unsigned seed = time(0);
    srand(seed);

    return 1 + rand() % 2;
  }

Second attempt which did work:

int main()
{
    unsigned seed = time(0);
    srand(seed);

    coinToss();
 }

 int coinToss()
 { 
    return 1 + rand() % 2;
  }
  • 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-18T20:06:16+00:00Added an answer on May 18, 2026 at 8:06 pm

    You probably only want to seed the random number generator once. rand() returns the next pseudo-random number from it’s internal generator. Every time you call rand() you will get the next number from the internal generator.

    srand() however sets the initial conditions of the random number generator. You can think of it as setting the ‘starting-out point’ for the internal random number generator (in reality it’s a lot more complicated than that, but it’s a useful cognitive model to follow).

    So, you should be calling srand(time(0)) exactly once in your application – somewhere near the beginning. After that, you can call rand() as many times as you want!

    However

    To answer your actual question – the first version doesn’t work because time() returns the number of seconds since the epoch. So If you call coinToss() several times in a second (say, if you wanted to simulate 100 coin tosses), then you’d be constantly seeding the random number generator with the same number, thereby resetting it’s internal state (and thus the next number you get) every time.

    Anyway – using time() as a seed to srand() is somewhat crappy for this very reason – time() doesn’t chage very often, and worse, it’s predictable. If you know the current time, you can work out what rand() will return. The internet has many, many examples of better srand() seeds.

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

Sidebar

Related Questions

I was creating a simple program that simulates a coin toss for my class.
So I'm working on creating a very simple C program that just preforms shell
We have a fairly simple program that's used for creating backups. I'm attempting to
I am teaching myself Common Lisp and I'm creating a simple program that interfaces
I am making simple ANSI C program, that simulates Unix shell. So I am
I am trying to build a simple openGL and SDL program that just refuses
I would like to learn about creating a program that I could draw simple
A lot of time when creating simple events in my program that other classes
I'm creating a simple program with the possibility to change the language and I
When creating a simple Flex 4.6 App, where the creationComplete-handler looks like this: protected

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.