Generate Random Numbers Between Two Numbers in Objective-C

后端 未结 6 1712
遇见更好的自我
遇见更好的自我 2020-12-07 19:07

I have two text boxes and user can input 2 positive integers (Using Objective-C). The goal is to return a random value between the two numbers.

I\'ve used \"man arc4

相关标签:
6条回答
  • 2020-12-07 19:09

    Objective-C Function:

    -(int)getRandomNumberBetween:(int)from to:(int)to
    {
        return (int)from + arc4random() % (to-from+1);
    }
    

    Swift:

    func getRandomNumberBetween(_ from: Int, to: Int) -> Int 
    {
        return Int(from) + arc4random() % (to - from + 1)
    }
    

    Call it anywhere by:

    int OTP = [self getRandomNumberBetween:10 to:99];
    NSLog(@"OTP IS %ld",(long)OTP);
    NSLog(@"OTP IS %@",[NSString stringWithFormat @"%ld",(long)OTP]);
    

    For Swift:

    var OTP: Int = getRandomNumberBetween(10, to: 99)
    
    0 讨论(0)
  • 2020-12-07 19:09

    In Swift:

    let otp = Int(arc4random_uniform(6))
    

    Try this.

    0 讨论(0)
  • 2020-12-07 19:19

    The following code include the minimum AND MAXIMUM value as the random output number:

    - (NSInteger)randomNumberBetween:(NSInteger)min maxNumber:(NSInteger)max
    {
        return min + arc4random_uniform((uint32_t)(max - min + 1));
    }
    

    Update:
    I edited the answer by replacing arc4random() % upper_bound with arc4random_uniform(upper_bound) as @rmaddy suggested.
    And here is the reference of arc4random_uniform for the details.

    Update2:
    I updated the answer by inserting a cast to uint32_t in arc4random_uniform() as @bicycle indicated.

    0 讨论(0)
  • 2020-12-07 19:28

    You should avoid clamping values with mod (%) if you can, because even if the pseudo-random number generator you're using (like arc4random) is good at providing uniformly distributed numbers in its full range, it may not provide uniformly distributed numbers within the restricted modulo range.

    You also don't need to use a literal like 0x100000000 because there is a convenient constant available in stdint.h:

    (float)arc4random() / UINT32_MAX
    

    That will give you a random float in the interval [0,1]. Note that arc4random returns an integer in the interval [0, 2**32 - 1].

    To move this into the interval you want, you just add your minimum value and multiply the random float by the size of your range:

    lowerBound + ((float)arc4random() / UINT32_MAX) * (upperBound - lowerBound);
    

    In the code you posted you're multiplying the random float by the whole mess (lowerBound + (upperBound - lowerBound)), which is actually just equal to upperBound. And that's why you're still getting results less than your intended lower bound.

    0 讨论(0)
  • -(int) generateRandomNumberWithlowerBound:(int)lowerBound
                                   upperBound:(int)upperBound
    {
        int rndValue = lowerBound + arc4random() % (upperBound - lowerBound);
        return rndValue;
    }
    
    0 讨论(0)
  • 2020-12-07 19:33

    You could simply use integer values like this:

    int lowerBound = ...
    int upperBound = ...
    int rndValue = lowerBound + arc4random() % (upperBound - lowerBound);
    

    Or if you mean you want to include float number between lowerBound and upperBound? If so please refer to this question: https://stackoverflow.com/a/4579457/1265516

    0 讨论(0)
提交回复
热议问题