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. Random!!!!!

Random!!!!!

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelplounge
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.
  • H Offline
    H Offline
    hkl
    wrote on last edited by
    #1

    Hi, I've found this equation in here: #define Randomize(min, max) ((rand() % (int)(((max) + 1) - (min))) + (min)) //get a random number between min and max and I realized that the result generated are the same each time, so I've added this before calling the above equation: srand((UINT)time(NULL)); But then, when I use multithread, (e.g. 2 threads), they call the Randomize function and the one above, and generate the exact same result. So, my question is: How can I get two different results when 2 threads are created at the same time?? (By the way, I've also tried Sleep(Randomize(....)) to let them start at different time...) PLEASE HELP!!! THANKS!!!

    J 1 Reply Last reply
    0
    • H hkl

      Hi, I've found this equation in here: #define Randomize(min, max) ((rand() % (int)(((max) + 1) - (min))) + (min)) //get a random number between min and max and I realized that the result generated are the same each time, so I've added this before calling the above equation: srand((UINT)time(NULL)); But then, when I use multithread, (e.g. 2 threads), they call the Randomize function and the one above, and generate the exact same result. So, my question is: How can I get two different results when 2 threads are created at the same time?? (By the way, I've also tried Sleep(Randomize(....)) to let them start at different time...) PLEASE HELP!!! THANKS!!!

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      Try with srand(((unsigned)time(NULL))^GetCurrentThreadId()). PS: I substituted ^ for |. XOR is better suited for this purpose. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      H 1 Reply Last reply
      0
      • J Joaquin M Lopez Munoz

        Try with srand(((unsigned)time(NULL))^GetCurrentThreadId()). PS: I substituted ^ for |. XOR is better suited for this purpose. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

        H Offline
        H Offline
        hkl
        wrote on last edited by
        #3

        ;P :-D ;) FINALLY!!! I got stuck for so long!!

        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