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 7487965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T14:42:08+00:00 2026-05-29T14:42:08+00:00

I have a for loop that runs 15 times, with dh.setDoors() in every iteration.

  • 0

I have a for loop that runs 15 times, with dh.setDoors() in every iteration.

What setDoors does is call srand(time(0)), then whenever a random number is needed it’ll use, for example, carSetter = rand()%3+1. Alternatively, it may use decider = rand()%2+1.

Now, normally decider and carSetter are used in a different ways, but I suspected a problem and made it print out carSetter and decider at every iteration. Here’s what came out:

Door 1 has car
Decider is 2
Door 1 has car
Decider is 2
Door 1 has car
Decider is 2
Door 1 has car
Decider is 2
Door 1 has car
Decider is 2
etc...

The values ‘1’ and ‘2’ change when I run it multiple times, but are still the same throughout the 15 times.

Since the loop is running 15 different times, shouldn’t carSetter and decider print out a different random number every iteration?

When I don’t have srand(time(0)), it works as expected, but there’s no seed set, so it’s the same sequence of “random” numbers each time, so it’s probably a problem with the seed?

  • 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-29T14:42:09+00:00Added an answer on May 29, 2026 at 2:42 pm

    When you call srand(x), then the value of x determines the sequence of pseudo-random numbers returned in following calls to rand(), depending entirely on the value of x.

    When you’re in a loop and call srand() at the top:

    while (...) {
        srand(time(0));
        x = rand();
        y = rand();
    }
    

    then the same random number sequence is generated depending on the value that time(0) returns. Since computers are fast and your loop probably runs in less than a second, time(0) returns the same value each time through the loop. So x and y will be the same each iteration.

    Instead, you only usually need to call srand() once at the start of your program:

    srand(time(0));
    
    while (...) {
        x = rand();
        y = rand();
    }
    

    In the above case, x and y will have different values each time through the loop.

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

Sidebar

Related Questions

Suppose we have a loop that runs for 100 times. Does using unsigned char
I have some code that runs a model in a loop. Each iteration of
Short Question I have a loop that runs 180,000 times. At the end of
I have a loop that runs 47 times on my page. During the course
I have a loop that runs multiple times and is prone to exceptions (it
I have a loop that runs through a variety of websites and I'd like
I have a loop that runs for approx. 25 minutes i.e 1500 seconds. [100
I have a handler and a runnable, Handler has a loop that runs from
I have written a C# app that runs constantly in a loop and several
So I have a Linux program that runs in a while(true) loop, which waits

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.