# Generate a random number within range?

Possible Duplicate:

Generating Random Numbers in Objective-C

How do I generate a random number which is within a range?

### 6 Solutions Collect From Internet About “Generate a random number within range?”

This is actually a bit harder to get really correct than most people realize:

```
int rand_lim(int limit) {
/* return a random number between 0 and limit inclusive.
*/
int divisor = RAND_MAX/(limit+1);
int retval;
do {
retval = rand() / divisor;
} while (retval > limit);
return retval;
}
```

Attempts that just use `%`

(or, equivalently, `/`

) to get the numbers in a range almost inevitably introduce skew (i.e., some numbers will be generated more often than others).

As to why using `%`

produces skewed results: unless the range you want is a divisor of RAND_MAX, skew is inevitable. If you start with small numbers, it’s pretty easy to see why. Consider taking 10 pieces of candy and trying to divide it evenly between three children. Clearly it can’t be done — if you hand out all the candy, the closest you can get is for two kids to get three pieces of candy, and one of them getting four.

There’s only one way for all the kids to get the same number of pieces of candy: make sure you don’t hand out the last piece of candy at all.

To relate this to the code above, let’s start by numbering the candies from 1 to 10 and the kids from 1 to 3. The initial division says since there are three kids, our divisor is three. We then pull a random candy from the bucket, look at its number and divide by three and hand it to that kid — but if the result is greater than 3 (i.e. we’ve picked out candy number 10) we just don’t hand it out at all — we discard it and pick out another candy.

Of course, if you’re using a modern implementation of C++ (i.e., one that supports C++11 or newer), you should usually use one the `distribution`

classes from the standard library. The code above corresponds most closely with `std::uniform_int_distribution`

, but the standard library also includes `uniform_real_distribution`

as well as classes for a number of non-uniform distributions (Bernoulli, Poisson, normal, maybe a couple others I don’t remember at the moment).

```
int rand_range(int min_n, int max_n)
{
return rand() % (max_n - min_n + 1) + min_n;
}
```

For fractions:

```
double rand_range(double min_n, double max_n)
{
return (double)rand()/RAND_MAX * (max_n - min_n) + min_n;
}
```

http://www.cprogramming.com/tutorial/random.html

The second example shows how to generate by range

For an integer value in the range [min,max):

```
double scale = (double) (max - min) / RAND_MAX;
int val = min + floor(rand() * scale)
```

I wrote this specifically in Obj-C for an iPhone project:

```
- (int) intInRangeMinimum:(int)min andMaximum:(int)max {
if (min > max) { return -1; }
int adjustedMax = (max + 1) - min; // arc4random returns within the set {min, (max - 1)}
int random = arc4random() % adjustedMax;
int result = random + min;
return result;
}
```

To use:

```
int newNumber = [aClass intInRangeMinimum:1 andMaximum:100];
```

Add salt to taste

```
+(NSInteger)randomNumberWithMin:(NSInteger)min WithMax:(NSInteger)max {
if (min>max) {
int tempMax=max;
max=min;
min=tempMax;
}
int randomy=arc4random() % (max-min+1);
randomy=randomy+min;
return randomy;
}
```

I use this method in a random number related class I made. Works well for my non-demanding needs, but may well be biased in some way.

- Obj-C, How do I handle version number with 4 parts?, greater than etc
- Handling download of image using SDWebImage while reusing UITableViewCell
- “loaded nib but the view outlet was not set” exception
- topLayoutGuide in child view controller
- NSLocale currentLocale always returns “en_US” not user's current language
- Where does iPhone simulator put data files?
- Trying to populate NSMutableArray but get implicit conversion of int to id is disallowed with arc
- How to build project with unsupported class (new feature) in xcode and swift
- Objective C: Using UIImage for Stroking
- “No unexpired provisioning profiles found that contain any of the keychain's signing certificates” Horror
- Shorthand to test if an object exists in an array for Swift?
- finding out days remain from array of dates
- UITabBarController with diffent storyboard file for each ViewController of the TabBar
- Is there a way to select a segue anchor programmatically?
- Syntax error before '^' token