For God's sake, can someone please, CLEARLY, tell me how to generate a random INTEGER between 0 and 1 (i.e. 0 or 1). I have searched all over the net, and no one has put a good working code. I hate C++ and anyone who proposed/coded the "rand()" function/class/library. If anyone tell me to do this in Java, I'll do it in a blink of an eye, but this stupid C++ drove me crazy for the past week. If I made SO MANY combination of code, that if I put that much work on anything else, I may have invented or discover something new. So, please, someone, give me a correct piece of code that has TRULY been tested.
Here are everything I grabbed from the net and what I made.
srand(time(0)); //I tried NULL as well
int a = (float)rand() / RAND_MAX; OR
int a = (float)rand() / (RAND_MAX + 1); OR
int a = (double)rand() / (double)(RAND_MAX) OR
int r = (rand() % 1) OR
======
double a = (double)rand() / RAND_MAX;
int b = a;
I tried lots of other casting, but everything gives me only ZERO.
Rather than searching for recipes, you should think about the problem yourself for a minute. It's not hard. If you know anything about math, you know that taking the remainder of x/2 where x is an integer you get either 0 or 1 depending upon whether x is odd or even. All you need to do is find a source of random integers. rand() is just that.
If anyone tell me to do this in Java, I'll do it in a blink of an eye, but this stupid C++ drove me crazy for the past week. If I made SO MANY combination of code, that if I put that much work on anything else, I may have invented or discover something new.
roflmao - this post is hilarious in too many ways! My sides are hurting...
#include <time.h> // So we can use time() function
#include <iostream> // To output results to console
int main() // Main function required in all C++ programs and first function to be called
{
srand( time(NULL) ); //Randomize seed initialization
int randNum = rand() % 2; // Generate a random number between 0 and 1
std::cout << randNum; // Output the results to console
return 0; //Generate an "EXIT SUCCESS" return code
}
Clear enough for you? I find it hard to believe that you're so good at java but couldn't figure out something as simple as this, no offense.