Generate random numbers between two numbers in Objective-C

I have two text fields, and the user can enter 2 positive integers (using Objective-C). The goal is to return a random value between two numbers.

I used "man arc4random" and still can't completely wrap myself around. I came up with some kind of code, but it is buggy.

float lowerBound = lowerBoundNumber.text.floatValue; float upperBound = upperBoundNumber.text.floatValue; float rndValue; //if lower bound is lowerbound < higherbound else switch the two around before randomizing. if(lowerBound < upperBound) { rndValue = (((float)arc4random()/0x100000000)*((upperBound-lowerBound)+lowerBound)); } else { rndValue = (((float)arc4random()/0x100000000)*((lowerBound-upperBound)+upperBound)); } 

Right now, if I insert the values ​​0 and 3, everything seems to work fine. However, if I use the numbers 10 and 15, I can still get values ​​like 1.0000000 or 2.000000 for "rndValue".

Do I need to develop my own algorithm or do I need to change the way arc4random is used?

+44
random objective-c arc4random
Mar 13 '12 at 4:28
source share
5 answers

You can simply use integer values ​​as follows:

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

Or if you want to add a floating point number between lowerBound and upperBound? If yes, please refer to this question: https://stackoverflow.com/a/166778/

+114
Mar 13 2018-12-12T00:
source share
β€” -

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

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

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

Update2:
I updated the answer by inserting the listing in uint32_t in arc4random_uniform() , as shown in the figure.

+56
Dec 11 '13 at 7:36
source share
 -(int) generateRandomNumberWithlowerBound:(int)lowerBound upperBound:(int)upperBound { int rndValue = lowerBound + arc4random() % (upperBound - lowerBound); return rndValue; } 
+7
Oct 16 '14 at 12:00
source share

You can avoid pinching values ​​with mod (%) if you can, because even if the pseudo-random number generator that you use (e.g. arc4random ) is good for providing uniformly distributed numbers in the full range, it may not provide uniformly distributed numbers in within a limited range modulo.

You also don't need to use a literal like 0x100000000 , because in stdint.h:

there is a convenient constant
 (float)arc4random() / UINT32_MAX 

This 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 to the desired interval, you simply 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 multiply a random float by the entire mess (lowerBound + (upperBound - lowerBound)), which is actually just equal to upperBound. And why are you still getting less results than your estimated lower bound.

+2
Jul 19. '14 at 2:50
source share

Easy way from me:

Functions:

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

Call anywhere:

 int OTP = [self getRandomNumberBetween:10 to:99]; NSLog(@"OTP IS %ld",(long)OTP); 
0
Dec 28 '17 at 12:21
source share



All Articles