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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:37:27+00:00 2026-06-16T15:37:27+00:00

Here i used the following code to shuffle the NSMuttable Array, While shuffling The

  • 0

Here i used the following code to shuffle the NSMuttable Array, While shuffling The array repeats only one value and remaining all works properly, what change should i made to shuffle without repeating?

words = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3",@"4", nil] ;
NSUInteger count = [words count];

for (int i = 0; i < count; i++)
   {
    NSInteger nElements = count - i;
    NSInteger n = (arc4random() % nElements) + i;
    NSLog(@"n val %d i val %d",i,n);
    [words exchangeObjectAtIndex:i withObjectAtIndex:n];
   }

NSlog Output

2012-12-31 12:49:05.730 quizer[1607:c07] n val 0 i val 0
2012-12-31 12:49:05.731 quizer[1607:c07] n val 1 i val 3
2012-12-31 12:49:05.731 quizer[1607:c07] n val 2 i val 2
2012-12-31 12:49:05.732 quizer[1607:c07] n val 3 i val 3

Please Help me to solve

  • 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-16T15:37:28+00:00Added an answer on June 16, 2026 at 3:37 pm

    This code may help you,

    The shuffle array contains each number between 0 to TOTAL_NUMBER (your array size) only once.

    From which you can shuffle your existing array.

    @synthesize alreadyGeneratedNumbers;//this is a mutableArray.
    
    
    int TOTAL_NUMBER=5;//size of array
    
    
    -(int)generateRandomNumber{
        int low_bound = 0;
        int high_bound = TOTAL_NUMBER;
        int width = high_bound - low_bound;
        int randomNumber = low_bound + arc4random() % width;
        return randomNumber;
    }
    
    
    -(IBAction)randomNumbers:(UIButton *)sender
    {
    
        NSMutableArray *startArray=[NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
    
        NSMutableArray *shuffle = [NSMutableArray new];
    
        BOOL contains=YES;
        while ([shuffle count]<5) {
            NSNumber *generatedNumber=[NSNumber numberWithInt:[self generateRandomNumber]];
            //NSLog(@"->%@",generatedNumber);
    
            if (![alreadyGeneratedNumbers containsObject:generatedNumber]) {
                [shuffle addObject:generatedNumber];
                contains=NO;
                [alreadyGeneratedNumbers addObject:generatedNumber];
            }
        }
        if ([alreadyGeneratedNumbers count] >= TOTAL_NUMBER) {
            NSLog(@"\nOne shuffel ended.....what to reshuffle");
            [alreadyGeneratedNumbers removeAllObjects];
        }
    
        NSMutableArray *shuffledArray=[NSMutableArray new];
        for (NSNumber *index in shuffle) {
            [shuffledArray addObject:[startArray objectAtIndex:[index integerValue]]];
        }
        NSLog(@"Shuffled array = %@",shuffledArray);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've used the following code, taken from here , to create my own custom
here the following code is used to view the present modal view controller. [[self
I had ever used the following code snippet in one class. Today, I copied
I used the following code in one of my controller; if (Request.IsAjaxRequest()) { return
I used the following Java code for move the file form one directory to
I understand that the following code ( from here ) is used to read
I have used the following code to get click outside to close working: Here
This following piece of code is meant to create a multilevel array, print it,
I used the following code, but the DateTime field in SQL is represented as:
So, the idea is that I would take the following code used to run

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.