这一行代码是什么意思?

时间:2021-07-06 05:35:30

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.

gren_rand_uniform()是一个生成随机数0和1的函数。但是我不明白>。5和1。0的意思。

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

我知道这应该是一个基本的问题,请见谅。

Thanks!

谢谢!

7 个解决方案

#1


11  

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

由于gen_rand_uniform()可能在1到0之间生成均匀分布的随机数,所以值大于0.5的概率是50%。也就是说得到1或0的概率是50%

#2


12  

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

我不认为get_rand_uniform()做了您认为它做的事情。大概是这样的:

float get_rand_uniform(void);

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

或者翻倍。关键是,它返回一个0到1之间的随机小数。所以这个:

get_rand_uniform() > 0.5

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

是检查这个数字是否接近1或0。这:

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:

将随机浮点数四舍五入到1或0,如下:

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.

将随机选择的1或0赋给b,我认为这里没有必要用括号,但如果你喜欢的话,就用它。

#3


3  

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.

这是得到随机值的一种方法,它是1或0,每个50%的时间。“?”和“:”是条件操作符。

#4


3  

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

舍入。b变量不是0就是1。

#5


1  

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

它对抛硬币进行编码。(一种完全平衡的硬币。)

#6


0  

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.

你给我们的代码只是随机的bool。它将返回1或0。

#7


0  

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.

你在这里看到的是一个三元表达式。http://en.wikipedia.org/wiki/Ternary_operation(正如这里的其他人指出的)是一个条件结构,但是它是特定于表达式的,这意味着返回一个值。

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

这种结构存在于大多数语言中(但不在eg中)。并具有。的形式

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.

还要注意,条件可以是任何表达式(如您的例子gen_rand_uniform() > 0.5),并且实际上可以包含一个嵌套的三元表达式,它所要做的就是将值作为一个非false值进行计算。

#1


11  

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

由于gen_rand_uniform()可能在1到0之间生成均匀分布的随机数,所以值大于0.5的概率是50%。也就是说得到1或0的概率是50%

#2


12  

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

我不认为get_rand_uniform()做了您认为它做的事情。大概是这样的:

float get_rand_uniform(void);

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

或者翻倍。关键是,它返回一个0到1之间的随机小数。所以这个:

get_rand_uniform() > 0.5

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

是检查这个数字是否接近1或0。这:

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:

将随机浮点数四舍五入到1或0,如下:

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.

将随机选择的1或0赋给b,我认为这里没有必要用括号,但如果你喜欢的话,就用它。

#3


3  

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.

这是得到随机值的一种方法,它是1或0,每个50%的时间。“?”和“:”是条件操作符。

#4


3  

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

舍入。b变量不是0就是1。

#5


1  

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

它对抛硬币进行编码。(一种完全平衡的硬币。)

#6


0  

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.

你给我们的代码只是随机的bool。它将返回1或0。

#7


0  

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.

你在这里看到的是一个三元表达式。http://en.wikipedia.org/wiki/Ternary_operation(正如这里的其他人指出的)是一个条件结构,但是它是特定于表达式的,这意味着返回一个值。

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

这种结构存在于大多数语言中(但不在eg中)。并具有。的形式

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.

还要注意,条件可以是任何表达式(如您的例子gen_rand_uniform() > 0.5),并且实际上可以包含一个嵌套的三元表达式,它所要做的就是将值作为一个非false值进行计算。