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
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. srand in c or c++ doesn't really Generate Random Numbers ?

srand in c or c++ doesn't really Generate Random Numbers ?

Scheduled Pinned Locked Moved Managed C++/CLI
helpc++tutorialquestionlounge
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.
  • S Offline
    S Offline
    snailflying
    wrote on last edited by
    #1

    Hi All I am using the srand function generate random numbers.Here is the problem. for example: #include #include int main() { int i = 0,j = 0; srand((int)time(0)); for(i=0; i<10; i++) { j=1+(int)(10.0*rand()/(RAND_MAX+1.0)); std::cout << j << '\t'; } std::cout << std::endl; return 0; } result: 5 8 8 8 10 2 10 8 9 9 2 9 7 4 10 3 2 10 8 7 The problem is that the two lines of the results are the same when the compiler run over in one second. How the function works? how the computer make the rand numbers? Does the function can generate really Random Numbers ? Please help me out if you find some solution for the above problem. Thanks in advancs ! Where did the good times go? Don't try it, just do it! *Archibald*rever dragon!

    P 1 Reply Last reply
    0
    • S snailflying

      Hi All I am using the srand function generate random numbers.Here is the problem. for example: #include #include int main() { int i = 0,j = 0; srand((int)time(0)); for(i=0; i<10; i++) { j=1+(int)(10.0*rand()/(RAND_MAX+1.0)); std::cout << j << '\t'; } std::cout << std::endl; return 0; } result: 5 8 8 8 10 2 10 8 9 9 2 9 7 4 10 3 2 10 8 7 The problem is that the two lines of the results are the same when the compiler run over in one second. How the function works? how the computer make the rand numbers? Does the function can generate really Random Numbers ? Please help me out if you find some solution for the above problem. Thanks in advancs ! Where did the good times go? Don't try it, just do it! *Archibald*rever dragon!

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Random numbers are very rarely random. They are defined using standard algorithms, and will return the same values based on the same seed value. Hence, they are known as pseudo-random numbers. If you need the sequences to differ, then you need to use a different seed value.

      Please visit http://www.readytogiveup.com/ and do something special today. Deja View - the feeling that you've seen this post before.

      S 1 Reply Last reply
      0
      • P Pete OHanlon

        Random numbers are very rarely random. They are defined using standard algorithms, and will return the same values based on the same seed value. Hence, they are known as pseudo-random numbers. If you need the sequences to differ, then you need to use a different seed value.

        Please visit http://www.readytogiveup.com/ and do something special today. Deja View - the feeling that you've seen this post before.

        S Offline
        S Offline
        snailflying
        wrote on last edited by
        #3

        thx a lot now i see! Where did the good times go? Don't try it, just do it! *Archibald*rever dragon!

        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