【问题标题】:iOS Random Number Generator to a new viewiOS 随机数生成器到新视图
【发布时间】:2012-03-23 07:45:22
【问题描述】:

我需要一些关于应用程序的帮助。我需要为 0 到 15 之间的整数创建一个随机数生成器,然后根据创建的数字,将其推送到具有相应数字的视图。 这就是我想要的工作方式

按下按钮 --> 随机数生成器给出一个介于 0 和 15 之间的数字 --> 视图推送到另一个视图,该视图已分配了随机数生成器给出的数字。

有人可以帮我写代码吗? 谢谢

【问题讨论】:

标签: objective-c ios xcode ipad


【解决方案1】:

arc4random() 是标准的 Objective-C 随机数生成器函数。它会给你一个介于零和......嗯,超过十五个之间的数字!您可以使用以下代码生成 0 到 15 之间的数字(因此,0、1、2、... 15):

NSInteger randomNumber = arc4random() % 16;

然后你可以做一个 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];

或者更确切地说,在重新阅读问题后,它会如下所示:

UIViewController *viewController = [[MyViewController alloc] initWithNibName:@"MyViewController" 
viewController.number = randomNumber;

你会在 MyViewController 子类上有一个 NSInteger 属性。

【讨论】:

  • 首选函数 arc4random_uniform(),因为它不受模偏差的影响。
  • 这个函数会生成伪随机数吗?它使用什么种子?
  • @CharlesChow 引用手册页,"The arc4random() function uses the key stream generator employed by the arc4 cipher, which uses 8*8 8 bit S-Boxes. The S-Boxes can be in about (2**1700) states. The arc4random() function returns pseudo-random numbers in the range of 0 to (2**32)-1, and therefore has twice the range of rand(3) and random(3)."
【解决方案2】:

您可以使用arc4random_uniform

NSUInteger r = arc4random_uniform(16);

【讨论】:

  • 在 64 位模式下,arc4random_uniform 返回一个 32 位的 int,而 NSUInteger 是一个 64 位的 int,对吧?
  • u_int32_t arc4random_uniform(u_int32_t /*upper_bound*/) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3);看起来它始终是 32 位的无符号整数。
【解决方案3】:

根据Apple的说法,最好的方法是使用arc4random_uniform并通过上限:

arc4random_uniform(16)

来自文档:

arc4random_uniform() 将返回一个均匀分布的随机数 小于上限。建议使用 arc4random_uniform() 像 ``arc4random() % upper_bound'' 这样的结构,因为它避免了“模 偏差”,当上限不是 2 的幂时。

https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/arc4random.3.html

【讨论】:

    【解决方案4】:
        int randomIndex = arc4random() % 14 + 1 ; // gives no .between 1 to 15 ..
    
        switch (randomIndex)
    {
        case 0 :
        push view 1 ;
        break;
    
        case 1:
        ...
    
    }
    

    【讨论】:

    • arc4random() % 16 匹配正确的范围,arc4random() % 14 + 1 也给出[1,14]
    • 为什么是-1?除了 % 14 + 1;我看不出有什么问题。
    【解决方案5】:

    在 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)
    

    【讨论】:

      【解决方案6】:

      我们可以为此使用 C 函数 rand()

      这会生成一个介于 1 和 30 之间的整数。或者,您可以像这样使用 arc4random 函数:

      int i = arc4random() % 30;
      NSLog(@"Random Number: %i", i);
      

      【讨论】:

        【解决方案7】:
        extension CGFloat {
           static func random() -> CGFloat {
               return CGFloat(arc4random()) / CGFloat(UInt32.max)
           }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-12-06
          • 2013-01-24
          • 2023-01-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-03-14
          • 2021-12-07
          相关资源
          最近更新 更多