Thread: k really need help here ..please

  1. #1

    Unhappy k really need help here ..please

    srand((unsigned)time(null)); is not working for making real rnd numbers..any suggestions??

  2. #2
    Registered User
    Join Date
    Aug 2001
    Perhaps 'cause noone can't achieve real random numbers with a computer... though it should do just fine for a home programmer... Any code?
    kooma -

  3. #3
    You might try using NULL for the srand() command. C++ doesn't recognize it as "null".

  4. #4
    Registered User
    Join Date
    Feb 2002
    One comon mistake is that srand should only be used once.

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    ... and if none of that has helped, post your code.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  6. #6
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    srand() by itself won't return a random number. You must use srand() and rand() together.
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    int main()
       int c;
       srand( time(0) );
       c = 1 + rand() % 100;   // A random number from 1-100
      cout << "Your random number for today is " << c << endl;
      return 0;
    rand() by itself returns pseudo-random numbers. If you just used rand() by itself, it would return the same random numbers each time you ran the program. To "randomize" the rand() function you use the srand() function. This "seeds" or "randomizes" what the rand() function produces. I use the time() function to seed srand() so it is almost completley random.

  7. #7

    Talking thanks for replys..

    all had good points but its was the unsigned that was giving the grief.. many thanks to all for spending the time to reply

  8. #8
    Registered User
    Join Date
    Jan 2002
    What he said was right but if you divide by 100 the max int remainder you can have it 99

    "I once saw a photograph of a large
    herd of wild elephants in Central
    Africa seeing an airplane for the first
    time, and all in a state of wild
    collective terror... As, however, there
    were no journalists among them, the
    terror died down when the airplane
    was out of sight. "

    - Bertrand Russell

Popular pages Recent additions subscribe to a feed