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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:43:38+00:00 2026-06-01T09:43:38+00:00

Take the following program: #include <cstdlib> using std::rand; #include <iostream> using std::cout; int main()

  • 0

Take the following program:

#include <cstdlib>
using std::rand;

#include <iostream>
using std::cout;

int main()
{
    cout << rand() << ' ' << rand() << ' ' << rand() << '\n';
}

Due to rand producing the same values as long as the seed isn’t changed using srand, this should produce three identical numbers.
e.g.

567 567 567

However, when I run this program it gives me three different values.
e.g.

6334 18467 41

When the program is (compiled and) run again, the same three numbers are generated. Shouldn’t I have to use srand to change the seed before I start getting different results from rand? Is this just my compiler/implementation trying to do me a favour?

OS: Windows XP
Compiler: GCC 4.6.2
Libraries: MinGW

EDIT:
By trying to use srand, I discovered that this is the result from a seed of 1 (which I guess is made default).

  • 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-01T09:43:39+00:00Added an answer on June 1, 2026 at 9:43 am

    Each call to rand() will always generate a different random number.

    The seed actually determines the sequence of random numbers that’s created. Using a different seed will get you another 3 random numbers, but you will always get those 3 numbers for a given seed.

    If you want to have the same number multiple times just call rand() once and save it in a variable.

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

Sidebar

Related Questions

Suppose I have the following program: #include <stdio.h> int main() { printf(This is a
Please look at the following code listing: #include <iostream> using namespace std; class Base
Take the following : #include <stdio.h> main() { unsigned long long verybig = 285212672;
Take the following snippet: 1 #include <stdio.h> 2 #include <stdlib.h> 3 int foo(char [6]);
Take the following useless program: class Program { static void Main(string[] args) { IUnityContainer
Take a look at the following program: class Test { List<int> myList = new
I am compiling the following simple program with g++-4.6.1 --std=c++0x : #include <algorithm> struct
Take the following property: public string Foo { get; private set; } Using reflection,
The following program terminates correctly: import System.Random randomList = mapM (\_->getStdRandom (randomR (0, 50000::Int)))
#include <stdio.h> #include <ctype.h> #define STRING_LEN 500 void stripspaces(char, char, char); int main(void) {

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.