使用arc4random()时如何选择值范围 [英] How to select range of values when using arc4random()

查看:332
本文介绍了使用arc4random()时如何选择值范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用arc4random()时可以设置一系列数字吗?例如仅50-100。

Can I set a range of numbers when using arc4random()? For example 50-100 only.

推荐答案

正如下面其他帖子所指出的,最好使用 arc4random_uniform 。 (最初编写此答案时, arc4random_uniform 不可用)。除了避免 arc4random()%x 的模偏差外,它还可以避免在递归使用 arc4random 时出现播种问题时间短。

As pointed out in other posts below, it is better to use arc4random_uniform. (When this answer was originally written, arc4random_uniform was not available). Besides avoiding the modulo bias of arc4random() % x, it also avoids a seeding problem with arc4random when used recursively in short timeframes.

arc4random_uniform(4)

将生成0,1,2或3.因此您可以使用:

will generate 0, 1, 2 or 3. Thus you could use:

arc4random_uniform(51)

只需在结果中加50即可得到50& 5之间的范围。 100(含)。

and merely add 50 to the result to get a range between 50 & 100 (inclusive).

这篇关于使用arc4random()时如何选择值范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆