Thread: rapid random number generation problem

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    Quote Originally Posted by Newton View Post
    The cause of my problems is that I am an idiot.
    At least you realize it! I have said to myself more than once, with a rather irritated voice "You IDIOT! - What were you thinking?" [I even hit the keys hard when I wrote that to make it more emphasized, can you tell?]. As programming requires a certain level of detail mindedness, it happens that we make mistakes.
    Instead of calling srand once in the main program of my project I was calling it repeatedly in the function called from the main program. I have corrected this stupid error and removed the sleep() line and all is well.
    I did think that the cause of problem was something along those lines. And if you search the forum for "srand call only once" or some such, I'm pretty sure you'll find at least two posts THIS MONTH, and many more if you read through more. When novice programmers hear about srand, they think "Ah, it makes the random numbers more random, so calling it more must make it even better" (or something like that), and then wondering why when you call srand a dozen times in a second with a time that only chances once a second, it gives the same random number a dozen times in a row...

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Registered User
    Join Date
    Sep 2008
    Actually I had read a few quite a few "srand call only once" posts and of course everyone's helpful hints. The devil is in the details as they say. It wasn't my intention to continually call srand but that's what I was doing! All I can say is that I won't be making that mistake again.
    Thanks to everyone for their help - it did lead me to realize the errors of my ways,

  3. #18
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Visalia, CA, USA
    Well you didn't murder a kitten or anything. Its just going to slow down your code while performing a redundant task. People like me get paid good money to correct code where people have done similar mistakes. Knowledge is power and even had we not told you this fact I think through mere observation you would have eventually realized what was really going on here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random number issue
    By swgh in forum C++ Programming
    Replies: 15
    Last Post: 11-14-2008, 10:12 AM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  3. Problem with random number generation
    By HAssan in forum C Programming
    Replies: 1
    Last Post: 03-27-2007, 05:49 PM
  4. Random number generation
    By scrub05 in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2005, 08:20 AM
  5. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM

Tags for this Thread