简体   繁体   English

C中的偏置随机数生成器函数

[英]Biased random number generator function in C

Trying to create the following function in C: 尝试在C中创建以下函数:

bool randBool(double bias)

which returns either 0 or 1 randomly. 随机返回01

The part that is tripping me up is that I would like to allow for the user to input a "bias" in the range [-1.0, 1.0], which represents the likelihood that the output will be 0 or 1 . 绊倒我的部分是我想允许用户输入[-1.0,1.0]范围内的“偏差”,这表示输出为01的可能性。

Here are a few examples of how the inputted bias should influence the function: 以下是输入偏差应如何影响功能的几个示例:

======================================================= ================================================== =====

randBool(-1.0) should return 0 100% of the time. randBool(-1.0)应该100%返回0

randBool(1.0) should return 1 100% of the time. randBool(1.0)应该100%返回1

randBool(-0.5) is 50% more likely to return 0 than 1 . randBool(-0.5)返回0可能性比1 50%。

randBool(0.05) is 5% more likely to return 1 than 0 . randBool(0.05)返回1可能性比0 5%。

randBool(0.0) is no more likely to return 0 than 1 . randBool(0.0)不太可能返回0不是1

======================================================= ================================================== =====

I am almost certain that this is a probability problem, but I am not so familiar with the topic, so I am stumped on how to implement this function. 我几乎可以肯定这是一个概率问题,但我对这个主题并不熟悉,所以我对如何实现这个功能感到困惑。

Something like this : 像这样的东西:

bool randBool(double bias) {
    return rand() < ((RAND_MAX + 1.0) * ((bias + 1) / 2));
}

The ((bias + 1) / 2) part is to get the bias in the [0,1] range instead of [-1,1]. ((bias + 1) / 2)部分是在[0,1]范围而不是[-1,1]中得到偏差。 That could be avoided if the bias parameter is changed to already be in the [0,1] range. 如果bias参数已更改为已在[0,1]范围内,则可以避免这种情况。

The bias is then defined as the probability of a 1 being returned. 然后将偏差定义为返回1的概率。 With : 用:

  • 0.0 (corresponds to your bias -1.0 ) : all zeroes 0.0 (对应于你的偏差-1.0 ):全零
  • 0.25 (corresponds to your bias -0.5 ) : 25% ones, 75% zeroes 0.25 (对应于你的偏差-0.5 ):25%,75%零
  • 0.5 (corresponds to your bias 0.0 ) : even mix of ones and zeroes 0.5 (对应于你的偏差0.0 ):甚至混合了1和0
  • 0.525 (corresponds to your bias 0.05 ) : 52.5% ones, 47.5% zeroes 0.525 (对应于你的偏见0.05 ):52.5%,47.5%零
  • 1.0 (corresponds to your bias 1.0 ) : all ones 1.0 (对应你的偏见1.0 ):所有的


NOTES on RAND_MAX 关于RAND_MAX注意事项

  • If RAND_MAX < INT_MAX , then RAND_MAX + 1 can be used instead of RAND_MAX + 1.0 . 如果RAND_MAX < INT_MAX ,则可以使用RAND_MAX + 1而不是RAND_MAX + 1.0

  • If RAND_MAX + 1.0 cannot be represented by a double without rounding (ref. Are all integer values perfectly represented as doubles? ), then the presented solution is not reliable. 如果RAND_MAX + 1.0不能用double 精度表示 (参考。 所有整数值是否完美表示为双精度? ),那么所提出的解决方案是不可靠的。
    An approach on such platforms, could be to re-scale the result of rand() to a range that can be represented by either an int or a double (depending on whether you use RAND_MAX + 1 or RAND_MAX + 1.0 ). 在这样的平台上的方法可以是将rand()的结果重新缩放到可以用intdouble表示的范围(取决于你是使用RAND_MAX + 1还是RAND_MAX + 1.0 )。
    Or alternatively, use a different random number generator (that doesn't have this issue), which is probably a good idea anyway, given the many low quality implementations of rand() out there. 或者,使用不同的随机数生成器(没有这个问题),无论如何,这可能是一个好主意,因为rand()的许多低质量实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM