Thread: srand() question

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    100

    srand() question

    Hi,

    I have a program that takes a seed and generates a random number using srand and rand, however there is another requirement to it that I don't understand and am hoping that you may make sense of. The instructions say:

    The random number seed must be set using the srand function in stdlib.h. Random number must be generated by bitwise ANDing (&) 0x7FFF with the value return by the rand function in stdlib.h.
    Do you know what bitwise ANDing 0x7FFF is? The code I have is:

    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int seed;
        int randnum;
    
        seed = 65445321;
    
        cout << "Generating a random number...\n";
    
        srand(seed);
    
        randnum = rand();
    
        cout << randnum << "\n";
    
        system("Pause");
        return 0;
    }
    "I don't fail - I succeed at finding things that don't work"
    Website Promotion Techniques @AbstractPromotion.com

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    It means that to get each random value, do this:

    Code:
    random_value = rand() & 0x7FFF;

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Also, people usually seed srand() with the current time, otherwise you'd always get the same numbers every time you run the program:
    Code:
    srand( time() );

  4. #4
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    I have a quick question related to this. You guys, and everywhere else, always use time() as the seed for the random number generator. However, in my game programming book it always uses GetTickCount() as the seed. Will one of them generate numbers more randomly than the others?

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by mikeman118 View Post
    I have a quick question related to this. You guys, and everywhere else, always use time() as the seed for the random number generator. However, in my game programming book it always uses GetTickCount() as the seed. Will one of them generate numbers more randomly than the others?
    The seed doesn't influence how random the numbers are, only the particular sequence you end up getting. You just need to pass SOME value that isn't the same every time. Both time() and GetTickCount() work for that.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Also, GetTickCount() is a Windows specific function, so for portable code it's better to use time() since it's in all C/C++ platforms.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by cpjust View Post
    Also, people usually seed srand() with the current time, otherwise you'd always get the same numbers every time you run the program:
    Code:
    srand( time() );
    time() takes a parameter, so most people use
    Code:
    srand(time(0));
    To be completely proper, one should use
    Code:
    srand((unsigned)time(0));
    or, with C++-style casts,
    Code:
    srand(static_cast<unsigned>(time(0)));
    Some people like to use NULL instead of 0 as well.

    [edit] There's lots of information around. Google for "srand time". Here's one hit.
    http://www.thescripts.com/forum/thread214808.html [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Well, to be completely proper you need to generate a hash of the value returned by 'time', to take into account systems where the result is 64 bits, according to Prelude's website.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Oops, I forgot about the parameter. I don't need to use time() that much.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well, to be completely proper you need to generate a hash of the value returned by 'time', to take into account systems where the result is 64 bits, according to Prelude's website.
    Well, yes, it's true. http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Do you know what bitwise ANDing 0x7FFF is?
    That's called a bit mask. It "masks-off" bit-15 and above*, forcing those bits to zero. Bits 0-14 are unaffected.

    After masking, your random number cannot exceed be 7FFF hexadecimal (32767 decimal). Any numbers smaller than that will be unaffected. Higher-value numbers will be altered.


    *Note that bits are counted from zero, so "bit-15" is actually the 16th bit.

Popular pages Recent additions subscribe to a feed