iOS 随机数生成器到新视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9573395/
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
iOS Random Number Generator to a new view
提问by Sam
I need some help with an app. I need to make a random number generator for integers between zero and fifteen, which will then, depending on which number is created, push to a view with the corresponding number. This is how I want it to work
我需要一些有关应用程序的帮助。我需要为 0 到 15 之间的整数制作一个随机数生成器,然后根据创建的数字,将其推送到具有相应数字的视图。这就是我希望它工作的方式
Push a button --> random number generator gives a number between 0 and 15 --> view pushes to another view that has been assigned the number that the random number generator gave.
按下按钮 --> 随机数生成器给出一个 0 到 15 之间的数字 --> 视图推送到另一个已分配随机数生成器给出的数字的视图。
Can anybody help me with the code? Thanks
有人可以帮我写代码吗?谢谢
回答by Ash Furrow
arc4random()
is the standard Objective-C random number generator function. It'll give you a number between zero and... well, more than fifteen! You can generate a number between 0 and 15 (so, 0, 1, 2, ... 15) with the following code:
arc4random()
是标准的 Objective-C 随机数生成器函数。它会给你一个介于零到……嗯,超过十五个的数字!您可以使用以下代码生成 0 到 15(即 0、1、2、... 15)之间的数字:
NSInteger randomNumber = arc4random() % 16;
Then you can do a switch or a series of if
/else
statements to push a different view controller:
然后你可以做一个 switch 或一系列if
/else
语句来推送不同的视图控制器:
UIViewController *viewController = nil;
switch (randomNumber)
{
case 0:
viewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
break;
// etc ...
}
[self.navigationController pushViewController:viewController animated:YES];
Or rather, upon rereading the question, it would look like the following:
或者更确切地说,在重新阅读问题时,它看起来如下所示:
UIViewController *viewController = [[MyViewController alloc] initWithNibName:@"MyViewController"
viewController.number = randomNumber;
And you'd have an NSInteger
property on the MyViewController subclass.
并且您将NSInteger
在 MyViewController 子类上拥有一个属性。
回答by Rémy Virin
You can use arc4random_uniform
您可以使用 arc4random_uniform
NSUInteger r = arc4random_uniform(16);
回答by frodo2975
According to Apple, the best way is to use arc4random_uniform and pass the upper bound:
根据 Apple 的说法,最好的方法是使用 arc4random_uniform 并通过上限:
arc4random_uniform(16)
From the docs:
从文档:
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.
arc4random_uniform()将返回一个小于 upper_bound 的均匀分布的随机数。arc4random_uniform()被推荐用于类似 ``arc4random() % upper_bound'' 的结构,因为当上限不是 2 的幂时,它可以避免“模偏差”。
回答by Shubhank
int randomIndex = arc4random() % 14 + 1 ; // gives no .between 1 to 15 ..
switch (randomIndex)
{
case 0 :
push view 1 ;
break;
case 1:
...
}
回答by Balaji Malliswamy
In Swift 4.2, we don't have to call some "arc4random_uniform" function for creating random numbers, now we can just call a function "random(in:RANGE)".
在 Swift 4.2 中,我们不必调用一些“arc4random_uniform”函数来创建随机数,现在我们可以调用一个函数“random(in:RANGE)”。
//Create Random numbers Swift 4.2
//Int
let randomInt = Int.random(in: 1...10)
//Double
let radomDouble = Double.random(in: 1...10)
//Float
let randomFloat = Double.random(in: 1...10)
回答by Bhargav Sejpal
extension CGFloat {
static func random() -> CGFloat {
return CGFloat(arc4random()) / CGFloat(UInt32.max)
}
}
回答by Bhargav Sejpal
We can use the C function rand()
for this:
我们可以rand()
为此使用 C 函数:
This generates an integer between 1 and 30. Alternatively you can use the arc4random
function like this:
这会生成一个 1 到 30 之间的整数。或者,您可以使用如下arc4random
函数:
int i = arc4random() % 30;
NSLog(@"Random Number: %i", i);