14

I want to get random object from array, is there any way how can I find random object from mutable array?

Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
Sonu
  • 937
  • 1
  • 10
  • 39
  • possible duplicate of [how to access randon items from an array in i phone sdk](http://stackoverflow.com/questions/3509411/how-to-access-randon-items-from-an-array-in-i-phone-sdk) – Brad Larson Sep 29 '11 at 01:16

7 Answers7

36
@interface NSArray (Random)
- (id) randomObject;
@end

@implementation NSArray (Random)

- (id) randomObject
{
     if ([self count] == 0) {
         return nil;
     }
     return [self objectAtIndex: arc4random() % [self count]];
}

@end
zoul
  • 102,279
  • 44
  • 260
  • 354
  • 2
    To avoid modulo bias, rather than arc4random() use arc4random_uniform(). (For more, see http://stackoverflow.com/questions/10984974/why-do-people-say-there-is-modulo-bias-when-using-a-random-number-generator). – Graham Perks May 21 '14 at 21:34
8
id obj;    
int r = arc4random() % [yourArray count];
    if(r<[yourArray count])
      obj=[yourArray objectAtIndex:r];
   else
   {
     //error message
   }
Ishu
  • 12,797
  • 5
  • 35
  • 51
7
id randomObject = nil;
if ([array count] > 0){
    int randomIndex = arc4random()%[array count];
    randomObject = [array objectAtIndex:randomIndex];
}
Vladimir
  • 170,431
  • 36
  • 387
  • 313
3

The best way would be to do something like this

int length = [myMutableArray count];
// Get random value between 0 and 99
int randomindex = arc4random() % length;

Object randomObj = [myMutableArray objectAtIndex:randomindex];
Rahul Choudhary
  • 3,789
  • 2
  • 30
  • 30
1

Just Copy and Paste

-(NSMutableArray*)getRandomValueFromArray:(NSMutableArray*)arrAllData randomDataCount:(NSInteger)count {  
 NSMutableArray *arrFilterData = [[NSMutableArray alloc]init];
for(int i=0; i<count; i++){

     NSInteger index = arc4random() % (NSUInteger)(arrAllData.count);
    [arrFilterData addObject:[arrAllData objectAtIndex:index]];
    [arrAllData removeObjectAtIndex:index];
}
return arrFilterData;
}

Note: Count = number of random values you want to fetch

Alok
  • 24,880
  • 6
  • 40
  • 67
1

Here's a Swift solution using an extension on Arrays:

extension Array {
    func sample() -> Element? {
        if self.isEmpty { return nil }
        let randomInt = Int(arc4random_uniform(UInt32(self.count)))
        let randomIndex = self.startIndex.advancedBy(randomInt)
        return self[randomIndex]
    }
}

You can use it as simple as this:

let digits = Array(0...9)
digits.sample() // => 6

If you prefer a Framework that also has some more handy features then checkout HandySwift. You can add it to your project via Carthage then use it exactly like in the example above:

import HandySwift    

let digits = Array(0...9)
digits.sample() // => 8
Jeehut
  • 20,202
  • 8
  • 59
  • 80
0

If you don't want to extend NSArray this will get a random value from a given array in one line:

id randomElement = [myArray objectAtIndex:(arc4random() % myArray.count)];
Max
  • 1,468
  • 10
  • 16