Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6687925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:20:43+00:00 2026-05-26T05:20:43+00:00

I’ve NSArray with over 100 strings. I would like to pick 4 different strings

  • 0

I’ve NSArray with over 100 strings.
I would like to pick 4 different strings randomly. I can write traditional way of code using for/while loops and get the task done.

But is there any better way to pick 4 different random strings?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:20:43+00:00Added an answer on May 26, 2026 at 5:20 am

    I wrote some utils as category on NSArray.

    You could use it like this:

    #import "NSArray+RandomUtils.h"
    
    NSArray *array = [NSArray arrayWithObjects:@"aa", @"ab",@"c",@"ad",@"dd", nil];
    NSSet *set = [array setWithRandomElementsSize:4];
    

    This will give you a set of 4 unique random elements.

    If you want to allow objects to be in your collection more than once you can do:

    NSArray *array = [NSArray arrayWithObjects:@"aa", @"ab",@"c",@"ad",@"dd", nil];
    NSArray *resultArray = [array arrayWithRandomElementsSize:4];
    

    #import "NSArray+RandomUtils.h"
    
    @implementation NSArray (RandomUtils)
    
    -(NSMutableArray *)mutableArrayShuffled
    {
        NSMutableArray *array = [[self mutableCopy] autorelease];
        [array shuffle];
        return array;
    }
    
    -(NSMutableArray *)arrayShuffled
    {
        return [NSArray arrayWithArray:[self mutableArrayShuffled]];
    }
    
    -(id)randomElement
    {
        if ([self count] < 1) return nil;
        NSUInteger randomIndex = arc4random() % [self count];
        return [self objectAtIndex:randomIndex];
    }
    
    -(NSSet *)setWithRandomElementsSize:(NSUInteger)size
    {
        if ([self count]<1) return nil;
        if (size > [self count]) 
            [NSException raise:@"NSArrayNotEnoughElements" 
                        format:@"NSArray's size (%d) is too small to fill a random set with size %d", [self count], size];
    
        NSMutableSet *set = [NSMutableSet set];
        NSMutableArray *array = [self mutableArrayShuffled];
    
        if (size == [array count]) 
            return [NSSet setWithArray:array];
    
        while ([set count]< size) {
            id object = [array objectAtIndex:0];
            [array removeObjectAtIndex:0];
            [set addObject:object];
    
        }
        return [NSSet setWithSet:set];
    }
    
    -(NSArray *)arrayWithRandomElementsSize:(NSUInteger)size
    {
        if ([self count]<1) return nil;
    
        NSMutableArray *array = [NSMutableArray array];
        while ([array count] < size) {
            [array addObject:[self randomElement]];
        }
        return  [NSArray arrayWithArray:array];
    }
    @end
    
    @implementation NSMutableArray (RandomUtils)
    -(void)shuffle
    {
    
        NSUInteger count = [self count];
        for (NSUInteger i = 0; i < count; ++i) {
            NSUInteger nElements = count - i;
            NSUInteger n = (arc4random() % nElements) + i;
            [self exchangeObjectAtIndex:i withObjectAtIndex:n];
        }
    }
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
Does anyone know how can I replace this 2 symbol below from the string
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.