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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:41:06+00:00 2026-06-16T20:41:06+00:00

Consider this body of main : std::srand(std::time(nullptr)); while (std::rand()); Surprisingly, I couldn’t find anything,

  • 0

Consider this body of main:

std::srand(std::time(nullptr));
while (std::rand());

Surprisingly, I couldn’t find anything, whether it be in the spec, on Google, or on this website, about whether that is well-defined. As for the spec:

N3485 § 6.5/4 [stmt.iter] says this about the condition:

[ Note: The requirements on conditions in iteration statements are described in 6.4. — end note ]

However, looking through 6.4, I saw nothing that refers to this scenario. In theory, the loop could virtually go on forever, but in practice, I usually had a run time of 5ms, with one time in all test runs being 22ms.

Is it well-defined behaviour to base the loop termination condition on a changing (pseudo)random number? If it isn’t, which kind of behaviour is it?

  • 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-06-16T20:41:08+00:00Added an answer on June 16, 2026 at 8:41 pm

    std::rand() will be called at each iteration, then the loop will go on or not depending on its return value.

    There is no reason to have any undefined behavior here.

    It’s not different from doing something like this

    while (my_vector.empty());
    

    Why do you think this case would be special?

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

Sidebar

Related Questions

Consider this example: #include <algorithm> #include <iostream> int main() { std::string str = abcde4fghijk4l5mnopqrs6t8uvwxyz;
Consider this simple markup: <body> <div style=border: 2px solid navy; position:absolute; width:100%; height:100%> </div>
This problem is only in IE. Consider the following HTML: <html> <body> <div style='position:absolute;left:1em;right:1em;top:1em;bottom:1em;overflow:auto;>
Consider this template function: template<typename ReturnT> ReturnT foo(const std::function<ReturnT ()>& fun) { return fun();
Consider this being slipped into some HTML file right after a <body> tag and
Consider this page: http://losthobbit.net/temp/docking.html Here's the HTML: <html> <head> <title></title> </head> <body> <div style=position:
Please consider this example: ( working example ) Style declarations: body {background:#333;font:1em Arial, Helvetica,
Consider this unhygienic Scheme macro: (define-macro for (lambda (i i1 i2 . body) (let
Here's a simple puzzle that's been frustrating me for a while today: Consider this
Consider this code: enum { ERR_START, ERR_CANNOTOPENFILE, ERR_CANNOTCONNECT, ERR_CANNOTCONNECTWITH, ERR_CANNOTGETHOSTNAME, ERR_CANNOTSEND, }; char* ERR_MESSAGE[]

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.