How do I use this, and how do I specify it to display a rand number either 0 or 1?
How do I use this, and how do I specify it to display a rand number either 0 or 1?
srand() is usually placed on the top of int main ()
Here is an example.
Hope This Helps.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. }
-Kyle
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
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.Originally Posted by bikr692002
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
I have a question, what is the difference between just an int and a signed or unsigned int?
"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
Thanks for clearing that up. Much appreciatedOriginally Posted by major_small
Thanks man, works perfectly, and doesn't delete my source like what you tricked me in to before... oh well, I was dumb
Originally Posted by comwizSo you can store a much larger number in an unsigned int, while taking up the same amount of memory as a regular 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
The range is exactly the same though.
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.
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
That's the case on most 32-bit compilers, but that's not what the standard specifies. The standard specifies:just an int(4 bytes):
-2,147,483,648 to 2,147,483,647
unsigned int(4 bytes):
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.)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
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.
So how would I make it choose between 1-100?