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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:30:54+00:00 2026-05-15T23:30:54+00:00

This code #include <stdio.h> #include <stdlib.h> #include <time.h> int main () { printf (First

  • 0

This code

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

int main ()
{
    printf ("First number: %d\n", rand() % 100);
    srand ( time(NULL) );
    printf ("Random number: %d\n", rand() % 100);
    srand ( 1 );
    printf ("Again the first number: %d\n", rand() %100);

    return 0;
}

has the following output:

First number: 41
Random number: 13
Again the first number: 41

There is also the following rule:

Two different initializations with the same seed, instructs the pseudo-random generator to generate the same succession of results for the subsequent calls to rand in both cases.

I understand the words but I just don’t understand the method itself. Why did it return 41 again? Is it random or must it return the same result in every case according to this code?

  • 1 1 Answer
  • 1 View
  • 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-15T23:30:56+00:00Added an answer on May 15, 2026 at 11:30 pm

    If you call rand() without first calling srand(), the behavior is as if you called srand() with 1 as the argument.

    This behavior is defined in the original C standard. I don’t have a full copy of it handy, but The Open Group’s POSIX standards are the next best thing, as they incorporate the full C standard (with some extensions):

    http://www.opengroup.org/onlinepubs/000095399/functions/rand.html

    The srand() function uses the argument as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to rand(). If srand() is then called with the same seed value, the sequence of pseudo-random numbers shall be repeated. If rand() is called before any calls to srand() are made, the same sequence shall be generated as when srand() is first called with a seed value of 1.

    The actual result of a rand() call for any given seed is implementation-defined, except that for any seed x, call n to rand() after the seeding of the generator must return the same result. So while on your implementation you will always get 41 on the first rand() call after calling srand(1), and you will always get the same result (whatever it is) for the second rand() call, other implementations may use a different algorithm that produces different results.

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

Sidebar

Related Questions

Please see this piece of code: #include<stdio.h> #include<string.h> #include<stdlib.h> int main() { int i
I have this code: #include <stdio.h> #include <openssl/sha.h> #include <openssl/ssl.h> int main(){ printf(OpenSSL version:
The code: #include stdafx.h #include stdio.h #include math.h #include stdlib.h #include time.h int main()
I wrote this tiny code: #include <stdio.h> int main() { size_t temp; temp =
I have this code which does the trick: #include <stdio.h> int main() { int
I wrote this code: #include <stdio.h> #include <stdlib.h> #include <time.h> #include <sys/types.h> #include <sys/shm.h>
can any one help in this program? #include <stdio.h> #include <stdlib.h> #include <time.h> int
This is my code: #include <stdio.h> #include <stdlib.h> #include <string.h> void getinfo(unsigned int a,
I'm trying to use SendInput() function. I wrote this code: #include <stdio.h> #include <stdlib.h>
I was under the impression that this code #include <windows.h> #include <stdio.h> int WINAPI

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.