srand()... possible reasons for failure

This is a discussion on srand()... possible reasons for failure within the C++ Programming forums, part of the General Programming Boards category; I have a confusing one here. My srand function appears to be failing. All my calls to rand() produce the ...

  1. #1
    Peace
    Join Date
    Aug 2001
    Posts
    1,510

    srand()... possible reasons for failure

    I have a confusing one here.

    My srand function appears to be failing. All my calls to rand() produce the same results as the previous compile. I've made the call to srand and it has worked perfectly... until now... reasons completly unknown.

    srand( (unsigned)time( NULL ) );

    The thing is, it doesnt matter what i initialize srand with. I could call

    srand(10) and the results remain the same as the previous compiles using time() to initialize srand.

    The app is DX7 (backwards compatibility is great as i'm using DX8 now). Its using D3D mostly with some old DX7 DDraw surfaces... VC++ 6.0... uhhh, Win98... cant think of anything else that might be relevant.

    It has worked just nicely for several months now and then *bang*. today it stopped... Cant remember what i'd been working on...

    Anyone ever seen this? What can cause srand() to fail? Are there any other possible reasons for these results? Anything i can try? Anything! Thanks.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  2. #2
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    I had the same problem with an airport simulation program I made. I never did figure out what I was doing wrong. You could make your own random function though.
    If a tree falls in the forest, and no one is around to see it, do the other trees make fun of it?

  3. #3
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    Try this-
    I know it's basically the same thing but maybe it'll work.
    Code:
    int main()
    {
     time_t CurrentTime;
    
     time(&CurrentTime);
     srand(CurrentTime);
     
     printf("%d\n", rand());
     return 0;
    }
    It worked for me, but then again so did the way you already have it. This way you could check for -1 from time() to make sure the time function worked.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    You can download a random number generator, which runs faster than rand on all machines/compilers/OSes I've tried it on, from here: http://www.math.keio.ac.jp/~matumoto/emt.html

    As for the srand problem - it's probably Visual C++'s fault. Have you tried creating a separate small project just to see if it works there? Also try selecting rebuild all from the build menu in VC++ and see if it works then.

    Hope this helps
    - lmov

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reasons why malloc fails?
    By prasath in forum C Programming
    Replies: 4
    Last Post: 04-06-2006, 12:09 AM
  2. Reasons for keeping functions small
    By maththeorylvr in forum C Programming
    Replies: 7
    Last Post: 03-26-2006, 12:38 AM
  3. can you explain reasons?
    By pacific in forum C Programming
    Replies: 2
    Last Post: 02-09-2005, 08:53 PM
  4. For what reasons would connect() fail?
    By Inquirer in forum C++ Programming
    Replies: 0
    Last Post: 11-29-2002, 04:14 PM
  5. a Few More Reasons two Hate MS.
    By no-one in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 09-29-2001, 07:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21