srand()

This is a discussion on srand() within the C++ Programming forums, part of the General Programming Boards category; How do I use this, and how do I specify it to display a rand number either 0 or 1?...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    241

    srand()

    How do I use this, and how do I specify it to display a rand number either 0 or 1?

  2. #2
    I Write C++ Apps, Sue Me.
    Join Date
    Feb 2006
    Location
    In My Computer
    Posts
    44
    srand() is usually placed on the top of int main ()

    Here is an example.
    Code:
    #include <iostream>
    #include <ctime>
    
    int main()
    {
    	//This Seeds rand with the time from process launch.
    	//I find that this is one of the best ways because.
    	//Two launches will never be the same.
    	time_t seconds;
    	time(&seconds);
    	srand((unsigned int) seconds);
    
    	std::cout<< rand(); //Prints A Random Number To The Screen.
    }
    Hope This Helps.

    -Kyle

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    you give srand a seed for the PRNG.

    it doesn't display a number... you need rand() for that.

    basically, here's what you want:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    int main()
    {
    	srand(static_cast<int>(time(0)));
    	std::cout<<rand()%2<<std::endl;
    	return 0;
    }
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    what does the %2 do?
    And the static_cast

  5. #5
    I Write C++ Apps, Sue Me.
    Join Date
    Feb 2006
    Location
    In My Computer
    Posts
    44
    Quote Originally Posted by bikr692002
    what does the %2 do?
    And the static_cast
    % 2 is the modulus Operator, It divides by 2 and checks for a remainder. The static_cast casts it as an integer.

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    yeah, I'm not exactly sure what the range of numbers that rand() returns is, but using the modulous operator helps you limit it. Basically, I limited it to either 0 or 1.

    static_cast is a C++ cast. In this case I casted what time() returns (a time_t type) into an unsigned integer because that's what rand() is seeded by. I use time because it's a seed that changes every time you run the program... unless you run the program several times in the same second...

    edit: oh, wait... I did cast it to an int... that shoud be <unsigned int> in that cast...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    I Write C++ Apps, Sue Me.
    Join Date
    Feb 2006
    Location
    In My Computer
    Posts
    44
    I have a question, what is the difference between just an int and a signed or unsigned int?

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    "just an int" is usually a signed int

    an unsigned int uses it's highest bit as data, and a signed int uses it's highest bit as a positive or negative sign.

    unsigned ints have a higher range, but it can only hold positive numbers.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  9. #9
    I Write C++ Apps, Sue Me.
    Join Date
    Feb 2006
    Location
    In My Computer
    Posts
    44
    Quote Originally Posted by major_small
    "just an int" is usually a signed int

    an unsigned int uses it's highest bit as data, and a signed int uses it's highest bit as a positive or negative sign.

    unsigned ints have a higher range, but it can only hold positive numbers.
    Thanks for clearing that up. Much appreciated

  10. #10
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Thanks man, works perfectly, and doesn't delete my source like what you tricked me in to before... oh well, I was dumb

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by comwiz
    I have a question, what is the difference between just an int and a signed or unsigned int?
    Code:
    just an int(4 bytes): 
    
    -2,147,483,648    to      2,147,483,647
    
    unsigned int(4 bytes):
    
    0        to         4,294,967,295
    So you can store a much larger number in an unsigned int, while taking up the same amount of memory as a regular int.

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    The range is exactly the same though.

  13. #13
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Tip: Instead of "n%2" for single random bit, try "n&1"

    With rand(), you probably won't need to worry about signed/unsigned int range, RAND_MAX (used by rand() to determine maximum size) should be only about 32768 in most implementations.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    I think RAND_MAX is less than or equal to INT_MAX . . . I guess it has to, since rand() returns an int. http://www.phim.unibe.ch/comp_doc/c_...C/MAN/rand.htm

    just an int(4 bytes):

    -2,147,483,648 to 2,147,483,647

    unsigned int(4 bytes):

    0 to 4,294,967,295
    That's the case on most 32-bit compilers, but that's not what the standard specifies. The standard specifies:
    Code:
    int: -32767 to +32767
    long: -2,147,483,647 to +2,147,483,647
    unsigned int: 0 to 65535
    unsigned long: 0 to 4,294,967,295
    A signed int is exactly the same as a int. The signed is implied. In fact, the standard only includes the keyword signed for symmetry with unsigned, and for chars. (A "char" can be either signed or unsigned, depending on the implementation, so if you actually want a char that can hold -100, you should use signed char.)
    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.

  15. #15
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    So how would I make it choose between 1-100?

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Use srand() twice in a function?
    By Jake.c in forum C Programming
    Replies: 5
    Last Post: 01-21-2009, 12:51 PM
  2. srand() in .Net
    By Sad Programmer in forum C++ Programming
    Replies: 6
    Last Post: 07-28-2003, 06:01 PM
  3. Same seed for srand yields different results
    By codegirl in forum C++ Programming
    Replies: 3
    Last Post: 06-23-2003, 03:39 PM
  4. When to srand()?
    By Imperito in forum C++ Programming
    Replies: 1
    Last Post: 05-12-2002, 01:20 AM
  5. srand()... possible reasons for failure
    By lightatdawn in forum C++ Programming
    Replies: 3
    Last Post: 12-18-2001, 02:33 AM

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