简体   繁体   中英

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?

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.

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);

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);

    }

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.

希望,它会有所帮助:

int r = arc4random()%30+30;

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))

Eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM