gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 07:03:47 25 4
gpt4 key购买 nike

我在看一个例子,这个家伙想要生成一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com