1

iphone有随机抽样功能吗?例如,如果你想掷一枚硬币,它的翻转次数有 25% 是正面的,你想把它翻转,看看这次你是否得到正面?我用谷歌搜索了 iphone 的随机抽样概率,但找不到任何东西。

4

1 回答 1

2

由于您可以在 Objective-C 中使用任何标准 C 函数,因此您可以使用drand48来获得 [0,1] 范围内的随机双精度数。通过测试该值是否 < p 来获得概率 p 的成功。前任:

if ( drand48() < 0.25 ){
     // This branch will be executed 25% of the time
} else {
     // This branch will be executed 75% of the time
}
于 2010-06-03T08:05:48.553 回答