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 9241893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:27:48+00:00 2026-06-18T08:27:48+00:00

I have created a quiz which draws questions from a plist into an array

  • 0

I have created a quiz which draws questions from a plist into an array and displays the question in a uilabel and the three possible answers in uibuttons. My question is how can I create a random order for the buttons so that the three answers don’t always show up in the same order?

Any help would be most appreciated.

Jamie

  • 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-06-18T08:27:49+00:00Added an answer on June 18, 2026 at 8:27 am

    Going along with @Abizern’s answer. It would be more efficient to have the fixed buttons and randomise the array. You could do something like this:

    //NSMutableArray *questions = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", @"4", nil];
    
    NSMutableArray *unorderedQuestions = [[NSMutableArray alloc] init];
    for (int i=0; i < [questions count]; i++) {
        int lowerBound = 0;
        int upperBound = 100;
        int rndValue = lowerBound + arc4random() % (upperBound - lowerBound);
        [unorderedQuestions addObject:[[NSArray alloc] initWithObjects:[questions objectAtIndex:i], [NSString stringWithFormat:@"%i", rndValue], nil]];
    }
    
    NSArray* sortedArray = [unorderedQuestions sortedArrayUsingFunction:order context:NULL];
    //or
    // questions = [unorderedQuestions sortedArrayUsingFunction:order context:NULL];
    NSLog(@"%@", sortedArray);
    

    and put this function somewhere:

    static NSInteger order (id a, id b, void* context) {
        NSString* catA = [a lastObject];
        NSString* catB = [b lastObject];
        return [catA caseInsensitiveCompare:catB];
    }
    

    This basically assigns a random number (between 0 and 100) to each question and sorts it by that number. You can then just make sortedArray global (or possibly just overwrite your questions array) and display objects sequentially and they will be shuffled.

    [btnA setTitle:[[[questions objectAtIndex:r] objectAtIndex:0] objectForKey:@"A"] forState:UIControlStateNormal];

    Judging by your code, you may need to amend a few lines: (but you could also create a currentQuestionsArray and assign it to [questions objectAtIndex:r] before the code above)

    for (int i=0; i < [questions count]; i++) {
    //to
    for (int i=0; i < [[questions objectAtIndex:r] count]; i++) {`
    

    and

    [unorderedQuestions addObject:[[NSArray alloc] initWithObjects:[questions objectAtIndex:i], [NSString stringWithFormat:@"%i", rndValue], nil]];
    //to
    [unorderedQuestions addObject:[[NSArray alloc] initWithObjects:[[questions objectAtIndex:r] objectAtIndex:i], [NSString stringWithFormat:@"%i", rndValue], nil]];`
    

    I think this is the most robust solution, as in the future you may have a different number of answers for a particular question and would not need to edit this to do so!

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

Sidebar

Related Questions

I'm making a flash quiz which will have a series of questions. Each question
I have created a quiz app which emails the user the results. I want
I have a quiz app which populates a sqlite database with questions of around
I have a questions.xml file which has a list of questions for my quiz,
I have created 3 classes as following Ext.mine.TextParent - Inherting from Textfield Ext.mine.child.TextChildA -
I've created Yii quiz module which I also want to use in Facebook app
I have created a form that allows users to make a multiple choice quiz.
I have this question in my online QUIZ since i am doing my degree
I have a quiz app which has a timer for the whole gameactivity where
I have created an android quiz application where i have a feature Exam .

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.