【问题标题】:Is there a random sampling function for iphone development that can simulate a coin toss?iphone开发有没有可以模拟抛硬币的随机抽样功能?
【发布时间】:2010-06-03 07:45:00
【问题描述】:

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

【问题讨论】:

  • 生成一个0到1之间的随机数,看看是不是小于0.25有什么问题?
  • 应该提到,作为答案 Tal

标签: iphone random probability random-sample


【解决方案1】:

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

if ( drand48() < 0.25 ){
     // This branch will be executed 25% of the time
} else {
     // This branch will be executed 75% of the time
}

【讨论】:

  • 我最终改用了 arc4random(),但这基本上就是我所做的。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2012-12-02
  • 2014-05-25
  • 2012-06-24
  • 1970-01-01
  • 1970-01-01
  • 2018-07-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多