user930195
user930195

Reputation: 432

Generating unique random number in objective c?

I want to generate a random number between 31 to 60.

So i have used rand() but i think it will give some time same value.But i need ebvery time it should give me new value.

How can i do this?

Upvotes: 2

Views: 7203

Answers (6)

iSankha007
iSankha007

Reputation: 395

the follwing method will generate an Array of unique random numbers in range low and high.as you mentioned in your question,you need random number between 31 to 60,so there will be maximum 29 unique numbers in the array.

 -(void)generateRandomUniqueNumberInRange :(int)rangeLow :(int)rangeHigh{
        NSMutableArray *unqArray=[[NSMutableArray alloc] init];
        int randNum = arc4random() % (rangeHigh-rangeLow+1) + rangeLow;
        int counter=0;
        while (counter<rangeHigh-rangeLow) {
            if (![unqArray containsObject:[NSNumber numberWithInt:randNum]]) {
                [unqArray addObject:[NSNumber numberWithInt:randNum]];
                counter++;
            }else{
                randNum = arc4random() % (rangeHigh-rangeLow+1) + rangeLow;
            }

        }
        NSLog(@"UNIQUE ARRAY %@",unqArray);

    }

Upvotes: 3

Groot
Groot

Reputation: 14251

Thought I could add a method I use in many projects.

- (NSInteger)randomValueBetween:(NSInteger)min and:(NSInteger)max {
    return (NSInteger)(min + arc4random_uniform(max + 1);
}

If I end up using it in many files I usually declare a macro as

#define RAND_FROM_TO(min,max) (min + arc4random_uniform(max + 1))

E.g.

NSInteger myInteger = RAND_FROM_TO(31,60) // 31, 32,..., 59, 60

Upvotes: 0

petervaz
petervaz

Reputation: 14175

Repeating values is consistent with random. If you can't repeat values, you can create an array with the possible values, and randomly remove a number from this array until it is empty, but this will be less random than a true random number.

Upvotes: 1

zaph
zaph

Reputation: 112857

Use arc4random(), it will provide a good random number and does not require seeding.

rand() will produce the same sequence each time the app is run unless you seed it with a unique value.

Then it is a matter or reducing the range. A simple solution is to mod to the range and add the low end of the range. (This will cause a small bias that should be acceptable.)

Example:

int rangeLow = 31;
int rangeHigh = 60;
int randomNumber = arc4random() % (rangeHigh-rangeLow+1) + rangeLow;

If what you want each number in the range to occur only once then @pawan.mangal has a link to a good solution.

Upvotes: 8

bilash.saha
bilash.saha

Reputation: 7296

Instead of rand() you can use arc4random()

To get an integer value from arc4random() that goes from 0 to x-1, you would do this:

int value = arc4random() % x;

To get an integer in the range 1 to x, just add 1:

int value = (arc4random() % x) + 1;

Finally, if you need to generate a floating point number, define this in your project:

#define ARC4RANDOM_MAX      0x100000000'

Then, you can use arc4random() to get a floating point value (at double the precision of using rand()), between 0 and 100, like so:

double val = floorf(((double)arc4random() / ARC4RANDOM_MAX) * 100.0f);

Upvotes: 4

beryllium
beryllium

Reputation: 29767

Hope, it will be helpful:

int r = arc4random()%30+30;

Upvotes: 1

Related Questions