Thread: Quick question about this source code.

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    18

    Quick question about this source code.

    This isn't my code. A 'friend' gave this to me to fix. I can't figure out why it keeps returning the same number when its suppose to be different each time(rand() is used).

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    My friend hasn't downloaded the source but he wonders if you have seeded the random number generator using srand(). You may want to check it out, but I wouldn't pay too much attention as this friend can be a real pain in the ass.
    Joe

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    38
    Joe is right on this one there is no srand(); in your code put that line in to fix your probelm.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    18
    Very funny. I'm serious I didn't code this. If I wanted to make a tetris game then I'd do it on the WinAPI since GDI makes graphics alot easier.

    I've looked at the source code and srand() has been used.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    38
    I didnt see a srand in there..... if he is running it threw the compiler it will usually give you the same number even if srand is used...

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    18
    Well, I gave up on the code.

    If you're interested AIM Jesusitron. Hes the guy who coded this. Give him the advice. Hes too stubborn, I tried giving him the link to this page but hes too busy trying to figure out whats wrong with his source code. Send him an instant message to grab his attention and help.

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    38
    I see the srand now... the fix is :
    1) put srand(clock()); in main
    2) on his srand he is using a constant a constant will ALWAYS generate the same number pattern

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    I've just given my friend a good seeing too (he no longer has any eyebrows). His next suggestion is

    make sure your only calling srand once

    If you're calling it within a certain timeframe you'll just keep seeding rand with the same number.

    I hope this is right, or there may be more than eyebrows at stake.
    Joe

  9. #9
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Random numbers are crappy. That's the bottom line.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need software to help to understand C source code
    By Kincider in forum C++ Programming
    Replies: 1
    Last Post: 09-28-2006, 09:44 PM
  2. starting linux's source code and kernel
    By sawer in forum Linux Programming
    Replies: 9
    Last Post: 08-01-2006, 07:46 AM
  3. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 12:22 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM