What does this line of code mean?

后端 未结 7 1030
抹茶落季
抹茶落季 2021-01-21 04:39

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

7条回答
  •  时光取名叫无心
    2021-01-21 05:08

    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

提交回复
热议问题