k really need help here ..please

This is a discussion on k really need help here ..please within the C++ Programming forums, part of the General Programming Boards category; srand((unsigned)time(null)); is not working for making real rnd numbers..any suggestions??...

  1. #1
    Unregistered
    Guest

    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
    Posts
    129
    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 - t.h@iki.fi

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

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    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
    Posts
    4,831
    ... 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
    Posts
    230
    srand() by itself won't return a random number. You must use srand() and rand() together.
    Code:
    #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;
      
      system("PAUSE");
      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
    Unregistered
    Guest

    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
    Posts
    16
    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

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