ios 使用 arc4random() 时如何选择值的范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3420581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to select range of values when using arc4random()
提问by NextRev
Can I set a range of numbers when using arc4random()? For example 50-100 only.
使用 arc4random() 时可以设置数字范围吗?例如仅 50-100。
回答by RunLoop
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
. (最初编写此答案时,arc4random_uniform
不可用)。除了避免 的模偏差外arc4random() % x
,它还避免了arc4random
在短时间内递归使用时的播种问题。
arc4random_uniform(4)
will generate 0, 1, 2 or 3. Thus you could use:
将生成 0、1、2 或 3。因此您可以使用:
arc4random_uniform(51)
and merely add 50 to the result to get a range between 50 & 100 (inclusive).
只需将 50 添加到结果中即可获得 50 和 100(含)之间的范围。
回答by Justyn
To expand upon JohnK comment.
扩展 JohnK 的评论。
It is suggested that you use the following function to return a ranged random number:
建议您使用以下函数返回一个范围随机数:
arc4random_uniform(51)
which will return a random number in the range 0
to 50
.
这将返回范围内的随机数0
到50
.
Then you can add your lower bounds to this like:
然后你可以添加你的下限,如:
arc4random_uniform(51) + 50
which will return a random number in the range 50
to 100
.
这将返回范围内的随机数50
到100
.
The reason we use arc4random_uniform(51)
over arc4random() % 51
is to avoid the modulo bias. This is highlighted in the man page as follows:
我们使用arc4random_uniform(51)
over的原因arc4random() % 51
是为了避免模偏差。这在手册页中突出显示如下:
arc4random_uniform(upper_bound) 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.
arc4random_uniform(upper_bound) 将返回一个小于 upper_bound 的均匀分布的随机数。arc4random_uniform() 被推荐用于类似 ``arc4random() % upper_bound'' 的结构,因为当上限不是 2 的幂时,它可以避免“模偏差”。
In short you get a more evenly distributed random number generated.
简而言之,您会得到一个更均匀分布的随机数。
回答by Vaibhav Saran
int fromNumber = 10;
int toNumber = 30;
int randomNumber = (arc4random()%(toNumber-fromNumber))+fromNumber;
Will generate randon number
between 10
and 30
, i.e. 11,12,13,14......29
将randon number
在10
和之间生成30
,即11,12,13,14......29
回答by handiansom
You can use this code for generating random values with range:
您可以使用此代码生成具有范围的随机值:
//range from 50 to 100
int num1 = (arc4random() % 50) + 50; or
int num1 = arc4random_uniform(50) + 50;
//range from 0-100
int num1 = arc4random() % 100; or
int num1 = arc4random_uniform(100);
回答by kurtanamo
In Swift you can use this (inspired by answer of @Justyn)
在 Swift 中,您可以使用它(灵感来自@Justyn的回答)
func generateRandomKey(fromRange rangeFrom:Int, toRange rangeTo:Int) -> Int{
let theKey = arc4random_uniform(UInt32(rangeTo - rangeFrom)) + UInt32(rangeFrom)
return Int(theKey)
}
Will always give you a random range Integer.
总是会给你一个随机范围的整数。
回答by Mike Young
In many situations 10 thru 30 would mean inclusive, (includes 10 and 30) ...
在许多情况下,10 到 30 表示包含,(包括 10 和 30)......
int fromNumber = 10;
int toNumber = 30;
toNumber ++;
int randomNumber = (arc4random()%(toNumber-fromNumber))+fromNumber;
Notice the difference toNumber - fromNumber is now 21 ... (20+1) which yields the possible results of 0 thru 20 (inclusive) which when added to fromNumber (10) results in 10 thru 30 (inclusive).
请注意 toNumber - fromNumber 现在的不同之处是 21 ... (20+1) 这会产生 0 到 20(含)的可能结果,当添加到 fromNumber (10) 时会产生 10 到 30(含)。