Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Shuffle NSMutableArray

VineeshVineesh Posts: 44Registered Users
edited July 2012 in iPhone SDK Tutorials
Iam started to make a simple quiz application in Xcode Viewbased application for iPhone.

I have 50 questions all questions are in the array. I have to select questions random with out repeat questions.

vineeshtp88@gmail.com

Thanks in Advance

Replies

  • the_stigthe_stig Posts: 41Registered Users @
    edited July 2012
    Best way to do that is just generate a random number then access the array item at that particular index.


    #include <stdlib.h> int numberOfItemsInArray = [arrayWithQuestions count]; //find out how many questions you have int randomNumber = arc4random() % numberOfItemsInArray; //generate a number less that or equal to the number of questions [arrayWithQuestion objectAtIndex:randomNumber]; //do with this what you will, this will return a random array item

    Note: This code is untested.




  • smithdale87smithdale87 Posts: 4,447iPhone Dev SDK Supporter @ @ @ @ @
    This does not prevent you from selecting the same question multiple times, though.
  • djbrooks111djbrooks111 Posts: 62Registered Users @
    edited July 2012
    I had wanted to do this as well and discovered how:

    randomPhrases is my NSMutableArray. The method shuffles the array and shows the array in the console once it is shuffled.
    -(void)shuffleQuestions {
        NSUInteger firstObject = 0;
        for (int i = 0; i < [randomPhrases count]; i++) {
            NSUInteger randomIndex = arc4random() % [randomPhrases count];
            [randomPhrases exchangeObjectAtIndex:firstObject withObjectAtIndex:randomIndex];
            firstObject += 1;
        }
        NSLog(@"Shuffled array:\n\n%@", randomPhrases);
    }
    Post edited by djbrooks111 on
    Need help with IAP? Use my drop-in class: https://github.com/djbrooks111/InAppPurchaseManager
  • the_stigthe_stig Posts: 41Registered Users @

    This does not prevent you from selecting the same question multiple times, though.

    I need to learn to read the problem better. lol. If it's an NSMutableArray you can just remove that item once you're finished though yeah?

  • VineeshVineesh Posts: 44Registered Users
    the_stig said:

    Best way to do that is just generate a random number then access the array item at that particular index.


    #include <stdlib.h> int numberOfItemsInArray = [arrayWithQuestions count]; //find out how many questions you have int randomNumber = arc4random() % numberOfItemsInArray; //generate a number less that or equal to the number of questions [arrayWithQuestion objectAtIndex:randomNumber]; //do with this what you will, this will return a random array item

    Note: This code is untested.




    It is repeating numbers.
  • djbrooks111djbrooks111 Posts: 62Registered Users @
    Here is my friendly reminder that I already posted the solution to properly shuffling a NSMutableArray in my above post :)
    Need help with IAP? Use my drop-in class: https://github.com/djbrooks111/InAppPurchaseManager
Sign In or Register to comment.