swift - arc4random_uniform 方法的文档在哪里?

标签 swift

我在看一个例子,这个家伙想要生成一个 1 到 100 之间的随机数。

targetValue= 1 + Int(arc4random_uniform(100))

然后他说:

You are also calling the function arc4random_uniform() to deliver an arbitrary integer (whole number) between 0 and 100. Actually, the highest number you will get is 99 because arc4random_uniform() treats the upper limit as exclusive. It only goes up-to 100, not up-to-and-including. To get a number that is truly in the range 1 - 100, you need to add 1 to the result of arc4random_uniform().

我没想到,所以我在 Apple Developer 网站上搜索了有关此功能的文档。对于我的生活,我找不到它。有人可以指出吗?

最佳答案

搜索“arc4random_uniform”时我在 Google 上的热门搜索结果:arc4random_uniform(3) .这是通过在终端中运行 man 3 arc4random_uniform 获得的相同文档。

相关部分:

u_int32_t arc4random_uniform(u_int32_t upper_bound);

arc4random_uniform() will return a uniformly distributed random number less than upper_bound. arc4random_uniform() is recommended over constructions like arc4random() % upper_bound as it avoids "modulo bias" when the upper bound is not a power of two.

如文档所述,上限是唯一的,因此如果您想包含上限,则需要增加上限或向上移动结果。

关于swift - arc4random_uniform 方法的文档在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49342992/

相关文章:

swift - 如何在结构中存储静态 DateFormatter?

swift - Swift 4 中未解析的标识符 'CALayerContentsGravity'

swift - 在 Swift wrt 消息类型中注册接收者对象

ios - Swift Tableview 获取特定单元格不工作

ios - 使用 Swift 限制 iOS 版 Google map 中的平移

ios - KeyboardWillShowNotification 和 UIAlert

swift - 带有图像的 UITableViewRowAction,swift

ios - 如何在 UIView 中缩放 CGPoints 和重新缩放

ios - 从 tabbarcontroller 内的 View Controller 转换时加载指示器

ios - 更改表格宽度时,Swift Tableview 会向上移动