How to do 50/50 chance of a number

closed account (16MjE3v7)
I am doing a Plinko program and when you drop a chip in, it has a 50/50 shot at either going left or right(which correspond to numbers such as 3.5 or 4).
How would you make a 50/50 chance of something equal either a 3.5 or a 4? Then it goes to the next stop with another 50/50 shot. So I know a loop is involved but how to start it? Thanks!
Maybe rand() % 2;
Maybe rand() % 2;

yes, but one correction, it should be:

 
(rand() % 2) + 1;


then, it should be like this:
1
2
3
4
5
bool tf = true; //flag, whether the first or second condition's happened

if ((rand() % 2) + 1 ==  2)
   tf = false
//otherwise, tf should be true 
Errr... why should you add 1 to it? That just makes it more complicated.

rand() % 2 gives you 0 or 1
(rand() % 2) + 1 gives you 1 or 2

Most programmers will (or at least should) feel more comfortable with zero-based numbers anyway.

Also, no need for an if statement either. Just assign to the boolean directly:

 
bool tf = (rand() % 2) != 0;
Topic archived. No new replies allowed.