Generating random numbers in Objective-C

前端 未结 13 2262
孤街浪徒
孤街浪徒 2020-11-22 02:07

I\'m a Java head mainly, and I want a way to generate a pseudo-random number between 0 and 74. In Java I would use the method:

Random.nextInt(74)
         


        
13条回答
  •  花落未央
    2020-11-22 02:36

    Better to use arc4random_uniform. However, this isn't available below iOS 4.3. Luckily iOS will bind this symbol at runtime, not at compile time (so don't use the #if preprocessor directive to check if it's available).

    The best way to determine if arc4random_uniform is available is to do something like this:

    #include 
    
    int r = 0;
    if (arc4random_uniform != NULL)
        r = arc4random_uniform (74);
    else
        r = (arc4random() % 74);
    

提交回复
热议问题