How do I use this, and how do I specify it to display a rand number either 0 or 1?
Printable View
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;
}
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.Quote:
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...
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.
Thanks for clearing that up. Much appreciatedQuote:
Originally 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
Quote:
Originally Posted by comwiz
So 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:Quote:
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
So how would I make it choose between 1-100?