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

  • SEARCH
  • Home
  • 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 858595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:33:47+00:00 2026-05-15T08:33:47+00:00

I’m trying to make an array of objects in random, non repeating order. Using

  • 0

I’m trying to make an array of objects in random, non repeating order. Using NSMutableSet for the heavy lifting as recommended here: How to check repetition of numbers in an array?

I’m dumping them into an NSArray after creation to access them, but the NSArray doesn’t stay in the order I placed them in the NSMutableSet. Making it more confusing is that it’s not consistent.

My code:

NSArray *puzzleImages = [[NSArray alloc] initWithObjects:@"elephant.png", @"gorilla.png", @"lion.png", @"zebra.png", @"flamingo.png", @"hyena.png", @"seal.png", @"hippo.png", @"rhino.png", @"tiger.png", @"macaw.png", @"bear.png", nil];

NSArray *puzzleSounds = [[NSArray alloc] initWithObjects:@"elephant", @"gorilla", @"lion", @"zebra", @"flamingo", @"hyena", @"seal", @"hippo", @"rhino", @"tiger", @"macaw", @"bear", nil];

NSMutableSet *aImages1 = [NSMutableSet setWithCapacity:2];
NSMutableSet *aSounds1 = [NSMutableSet setWithCapacity:2];
NSMutableSet *aValues1 = [NSMutableSet setWithCapacity:2];
while([aImages1 count]<=1){
  int Randnum1 = arc4random() % 11;
  [aImages1 addObject:[puzzleImages objectAtIndex:Randnum1]];
  [aSounds1 addObject:[puzzleSounds objectAtIndex:Randnum1]];
  [aValues1 addObject:[NSNumber numberWithInt:Randnum1]];
  NSLog(@"image:%@, sound:%@, value:%@",[puzzleImages objectAtIndex:Randnum1],[puzzleSounds objectAtIndex:Randnum1],[NSNumber numberWithInt:Randnum1]);
} 

NSArray *arrayOfImages1 = [aImages1 allObjects];
NSLog(@"arrayOfImages1: %@",arrayOfImages1);

NSArray *arrayOfSounds1 = [aSounds1 allObjects];
NSLog(@"arrayOfSounds1: %@",arrayOfSounds1);

NSArray *arrayOfValues1 = [aValues1 allObjects];
NSLog(@"aValues1: %@",aValues1);

Here’s my output:

2010-06-20 16:13:14.572 MatchGame[22675:207] image:lion.png, sound:lion, value:2
2010-06-20 16:13:14.574 MatchGame[22675:207] image:macaw.png, sound:macaw, value:10
2010-06-20 16:13:14.575 MatchGame[22675:207] arrayOfImages1: (
"lion.png",
"macaw.png")
2010-06-20 16:13:14.575 MatchGame[22675:207] arrayOfSounds1: (
macaw,
lion)
2010-06-20 16:13:14.576 MatchGame[22675:207] aValues1: {(
2,
10)}

How the heck did the Macaw sound end up above the lion sound? Course it doesn’t always happen, but it breaks the game when it does. I’m sure I’m missing something silly, but spent enough time trying to figure it out on my own.

  • 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-15T08:33:47+00:00Added an answer on May 15, 2026 at 8:33 am

    Sets are not ordered. In particular, the ordering you get out of them will depend on the objects you put in rather than the order you add them. (Often they are implemented using some kind of object hash, though I don’t know offhand if that’s the case here.)

    Since you want to keep a bunch of items linked, one option would be to make a single object class that contains them all, and store that in the set. You can then get it out and read off its different fields.

    Another option would be just to scramble one list of indices and then access the objects in your various original arrays in that order.

    Update: the post you link to is solving a rather different problem from yours. In your case, using set ordering to randomise is really the wrong thing to do, because the ordering will not be actually random. The right thing to do is to scramble a single array of indices, something like this:

    NSMutableArray *indices = [NSMutableArray arrayWithCapacity:11];
    for ( int i = 0; i < 11; ++i )
        [indices addObject:[NSNumber numberWithInt:i]];
    for ( int i = 0; i < 11; ++i )
        [indices exchangeObjectAtIndex:i withObjectAtIndex:(arc4random() % 11)];
    
    NSLog(@"First random image: %@", [puzzleImages objectAtIndex:[[indices objectAtIndex:0] intValue]]);
    

    And so on. Your guarantee of uniqueness comes from the fact that you start out with indices 0..10 in the list and then shuffle, rather than trying to conjure the indices themselves from a random number generator that may produce duplicates.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.