Random Integer 0 or 1

Hi,

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.
Last edited on
You're a rather obvious troll, but what the hell:
int a=rand()%2;
A quick Google search for "c++ random number generation" returned this http://www.learncpp.com/cpp-tutorial/59-random-number-generation/. Shouldn't be any trouble for a talented Java programmer like yourself. Isn't Google great?
Last edited on
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.
I hate C++


lol. Angry man is angry =]
Hey, before I learned C++ all random functions I had ever worked with returned a floating point value in [0,1)
floor( 2 * random() ); //example

So really it's just understanding that rand() returns something similar to Java's
new java.util.Random().nextInt();

Also, @ChangBroot: assert __ % 1 == 0;
Last edited on
ChangBroot wrote:
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...
LMFAO!
1
2
3
4
5
6
7
8
9
10
11
#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.
Last edited on
Topic archived. No new replies allowed.