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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:11:07+00:00 2026-05-23T13:11:07+00:00

This is a small program: #include <iostream> #include <cstdlib> using namespace std; int main()

  • 0

This is a small program:

#include <iostream>
#include <cstdlib>
using namespace std;
int main() {    
    long x = rand();
cout << x << endl;
}

It always displays 41.But if i modify the program like ,

#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
   for( int i = 0 ; i <= 9 ; i++ ) {    
    long x = rand();
cout << x << endl;
  }
}

The output is as expected.The set of random numbers.
OUTPUT:

41

18467

6334

26500

19169

15724

11478

29358

26962

24464

But why i get the same number when i run first program How does rand actually function ?

  • 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-23T13:11:07+00:00Added an answer on May 23, 2026 at 1:11 pm

    The random number generator built in compilers are usually pseudo random number generator. They typically use a recursive equation to generate next random number and use a seed to generate first random number. Check this link.

    To avoid having same random number as the first one, you need to change the seed. Again, same seed will give you same initial random number as well as same random number sequence. So you should change the seed every time you run your program/thread.

    To set the seed, you need to call srand(). The best way to change the seed value every time srand() is called is to use current timestamp: time(0).

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

Sidebar

Related Questions

Here's a small test program I wrote: #include <iostream> using namespace std; class A
Okay, I got this small program which tags (as in ID3v2.4 etc.) some music
As I stated in this question , I am using SDL for a small
I have a small program I want to execute to test something #include <map>
I have this small program that reads a line of input & prints the
header.h #include <iostream> #include <vector> class CombatLine{ std::stringstream Line; std::vector<std::string> TokenLine; void SetLine(std::string s){
I have a problem with this small program. It added some value to list.
I've got this small question - given a bitmask of weekdays (e.g., Sunday =
I have this small code library that I'm considering releasing into Open Source. I
Let's say I have a line of text like this Small 0.0..20.0 0.00 1.49

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.