Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. rand() always returns 18467...whats going on.. [modified]

rand() always returns 18467...whats going on.. [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
csharpvisual-studiodesignhelpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    montiee
    wrote on last edited by
    #1

    For the life of me I can't work this out. In the "apps" InitInstance I seed the random number generator by calling srand(time(NULL)); Then in a thread whenever I invoke rand() and print it out via cout it always has the value of 18467. Now rand() is invoked every minute or two so it's not like it's in a tight loop or anything like that. I never call srand again either. Just the once in the apps InitInstance function. Anyone experienced this problem before? Currently using Visual Studio 2008. I even went so far as to apply SP1 to it just incase something was fixed. Such a simple thing not working is really annoying me. Just to add if I print out rand() a few times in the apps InitInstance it works as expected. However once I call it from a UI thread it's always the same number.

    modified on Monday, September 1, 2008 3:30 PM

    M 1 Reply Last reply
    0
    • M montiee

      For the life of me I can't work this out. In the "apps" InitInstance I seed the random number generator by calling srand(time(NULL)); Then in a thread whenever I invoke rand() and print it out via cout it always has the value of 18467. Now rand() is invoked every minute or two so it's not like it's in a tight loop or anything like that. I never call srand again either. Just the once in the apps InitInstance function. Anyone experienced this problem before? Currently using Visual Studio 2008. I even went so far as to apply SP1 to it just incase something was fixed. Such a simple thing not working is really annoying me. Just to add if I print out rand() a few times in the apps InitInstance it works as expected. However once I call it from a UI thread it's always the same number.

      modified on Monday, September 1, 2008 3:30 PM

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      jbem wrote:

      In the "apps" InitInstance I seed the random number generator by calling srand(time(NULL));

      jbem wrote:

      Then in a thread whenever I invoke rand()

      srand() is thread specific. You need to seed (call srand()) on every thread that will call rand(). Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      M 1 Reply Last reply
      0
      • M Mark Salsbery

        jbem wrote:

        In the "apps" InitInstance I seed the random number generator by calling srand(time(NULL));

        jbem wrote:

        Then in a thread whenever I invoke rand()

        srand() is thread specific. You need to seed (call srand()) on every thread that will call rand(). Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        M Offline
        M Offline
        montiee
        wrote on last edited by
        #3

        Thanks. That did the trick.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups