tags:

views:

209

answers:

7

Hi, I am wondering what this line of code mean?

b = (gen_rand_uniform()>0.5)?1:0;

The gren_rand_uniform() is a function to generate random 0 and 1 numbers. However I don't get the meaning for >0.5 and 1:0.

I know this is supposed to be a basic question, please bear with me.

Thanks!

+3  A: 

it's a way to get a random value which is either 1 or zero, each 50% of the time. The "?" and ":" are the conditional operarator.

Justin Grant
+3  A: 

It's rounding. The b variable will either be 0 or 1.

Rob H
+1  A: 

It encodes a flip of the coin. (A perfectly balanced coin that is.)

Frank Krueger
+11  A: 

It's shorthand. In the example you gave, it is equivalent to:

if (gen_rand_uniform() > 0.5) {
    b = 1;
} else {
    b = 0;
}

Since gen_rand_uniform() probably generates uniformly distributed random numbers between 1 and 0, there's a 50% chance of the value being higher than 0.5. Which means there's a 50% chance of getting a 1 or a 0

Michiel Buddingh'
thanks for the answer! That cleared things up for me.
Victor
Its not shorthand, and its not exactly equivalent - it is in fact a separate construct.This can most easily be seen when using nested ternary expressions, and seing in what order they are evaluated, left to right or vice versa. Of course, some might say that behaving in any other way than that of an 'equivalent' if/else contstruct must be a bug.
Sean Kinsey
Thanks for your input; I've somewhat qualified my answer.
Michiel Buddingh'
That ternary operator is redundant; the effect of that statement is equivalent to `b = (gen_rand_uniform()>0.5);`
wilhelmtell
+11  A: 

I don't think get_rand_uniform() does what you think it does. It probably looks like this:

float get_rand_uniform(void);

Or maybe double. The point is, it returns a random decimal number between 0 and 1. So this:

get_rand_uniform() > 0.5

Is a check to see if that number is closer to 1 or 0. And this:

x ? y : z

Is the ternary conditional operator, which serves the same function as this:

if(x) { y } else { z }

Except that the ternary operator is an expression. So all of this:

get_rand_uniform() > 0.5 ? 1 : 0

Is basically rounding the random floating point number to 1 or 0, and this:

b = get_rand_uniform() > 0.5 ? 1 : 0;

Assigns that randomly picked 1 or 0 to b. I believe the parenthesis are unnecessary here, but if you like them, go for it.

Chris Lutz
Thanks for the detailed explanation. Really helped me :)
Victor
A: 

Conditional assignment:

variable = condition ? value_if_true : value_if_false;

which is equal to:

if (condition) {
    variable = value_if_true;
} else {
    variable = value_if_false;
}

The code you give us is just random bool. It will return either 1 or 0.

Andrejs Cainikovs
A: 

What you are seeing here is a ternary expression. http://en.wikipedia.org/wiki/Ternary_operation This is (as others here has pointed out) a conditional construct, but one that is specific to expressions, meaning that a value is returned.

This construct exist in most languages (but not in eg. VB.Net) and has the form of

condition ? valueiftrue: valueiffalse

An example of this in action is:

var foo = true;
var bar = foo ? 'foo is true' : 'foo is false';
// bar = 'foo is true'

Also note that the condition can be any expression (like in your case gen_rand_uniform() > 0.5) and can infact contain a nested ternary expression, all it has to do is evaluate as a non-false value.

Sean Kinsey