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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:24:09+00:00 2026-05-23T16:24:09+00:00

I don’t understand why srand() generates so similar random numbers between runs! I am

  • 0

I don’t understand why srand() generates so similar random numbers between runs!

I am trying to run the following code

srand ( time(NULL) );
int x = rand();
cout << x << endl;

However instead of a proper random number I always end up with almost the same number, which is growing slowly as the time goes. So I get numbers like: 11669, 11685, 11701, 11714, 11731.

What am I doing wrong?

I am using Visual Studio 2010 SP1.

OK, is srand() really that simple? I mean how would anyone call it a random function?

srand(1) => rand() = 41
srand(2) => rand() = 45
srand(3) => rand() = 48
srand(4) => rand() = 51
....
  • 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-23T16:24:10+00:00Added an answer on May 23, 2026 at 4:24 pm

    First, srand() isn’t a random function; it sets up the starting point
    of a pseudo-random sequence. And somewhat surprisingly, your
    implementation of rand() seems to be returning a value based on the
    previous state, and not on the newly calculated state, so that the first
    value after a call to srand() depends very much on the value passed to
    srand(). If you were to write:

    srand( time( NULL ) );
    rand();
    std::cout << rand() << std::endl;
    

    , I’m sure you’ll see a lot more difference.

    FWIW: I tried the following on both Windows and Linux:

    int
    main()
    {
        srand( time( NULL ) );
        int r1 = rand();
        std::cout << r1 << ' ' << rand() << std::endl;
        return 0;
    }
    

    Invoked 10 times at a one second interval, I got:

    16391 14979
    16394 25727
    16397 3708
    16404 25205
    16407 3185
    16410 13933
    16417 2662
    16420 13411
    16427 2139
    

    with VC++ under Windows—you’ll note the very low variance of the
    first call to rand()—and

    1256800221 286343522
    955907524 101665620
    1731118607 991002476
    1428701871 807009391
    44395298 1688573463
    817243457 1506183315
    507034261 1310184381
    1278902902 54648487
    2049484769 942368151
    1749966544 1833343137
    

    with g++ under Windows; in this case, even the first value read is
    relatively random.

    If you need a good random generator, you’ll probably have to use one
    from Boost; the standard doesn’t say much about what algorithm should be
    used, and implementations have varied enormously in quality.

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

Sidebar

Related Questions

Don't let below code scare you away . The question is really simple, only
Don't be scared of the extensive code. The problem is general. I just provided
I don't edit CSS very often, and almost every time I need to go
I don't understand where the extra bits are coming from in this article about
Don't know how to explain it better but i'm trying to get a response
Don't overlook the 'date AND TIME' part though.
Don't think that I'm mad, I understand how php works! That being said. I
Don't be frightened, its a very basic code. Just wanted to check with you
Don't know whats exactly going on, but it's definitely killing my time for nothing.
Don't they both have to convert to machine code at some point to execute

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.