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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:08:45+00:00 2026-06-05T03:08:45+00:00

Didn’t wanna just post, Hey, guys, can you come up with an awesome randomization

  • 0

Didn’t wanna just post, “Hey, guys, can you come up with an awesome randomization method for me?” so I’ve been studying algorithms for shuffling a deck of cards, creating truly random permutations, etc., ALL DAY LONG and it’s been rrrrrrrreally daunting. Finally settled on a simpler approach, shown below. While I do want to create a quality game, of course, I’m not going to be using this algorithm in an online casino or anything. It’s just an iOS game. So, is this random/memory-efficient enough? Or should I be giving this even MORE time and effort? TIA

Aside: Whenever I write “TIA,” I think, “This is Africa,” not “Thanks in advance.”

@implementation NSMutableArray (Shuffle)

-(void)shuffle
    {
        for (int i = [self count] - 1; i > 0; i--) {
            [self exchangeObjectAtIndex:(arc4random() % ([self count] - 1))
                      withObjectAtIndex:i];
        }
    }

    @end

EDIT:

Ok. Wanted to run some test code so I don’t post something dumb. 🙂 In response to Adam and Nielsbot, you’re recommending something more like this?

-(void)shuffle
{
    for (int i = [self count] - 1; i > 0; i--) {
        [self exchangeObjectAtIndex:(arc4random_uniform([self count] - 1))
                  withObjectAtIndex:i];
    }
}

Is that right?

Would there be any benefit to repeating it, like so?

-(void)shuffle
{
for (int i = [self count] - 1; i > 0; i--) {
    [self exchangeObjectAtIndex:(arc4random_uniform([self count] - 1))
              withObjectAtIndex:i];
}

for (int i = [self count] - 1; i > 0; i--) {
        [self exchangeObjectAtIndex:(arc4random_uniform([self count] - 1))
                  withObjectAtIndex:i];
    }
}
  • 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-05T03:08:47+00:00Added an answer on June 5, 2026 at 3:08 am

    That’s almost the so called Fisher-Yates algorithm, it’s O(n) as time complexity and memory efficience is not considerable since you don’t allocate anything.

    The only thing I’d change is the randomly chosen index that would be in range [0, i] for i-th iteration instead that the whole range (to avoid just swapping two elements twice), then it would become the algorithm I linked.

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

Sidebar

Related Questions

Didn't find any from their website. I mostly just trying to see which one
Didn't know how else to put this. Say I have a JavaScript method that
I didn't use Prototype.js before, when I use jquery, I can get an element
I didn't see a clear answer from the same question How can I figure
I didn't find any info on searches when I looked this up. I've been
Didn't Snow Leopard introduce some alternative to the old beginSheet: method that allows using
I didn't find this in the documentation . Should I just make the close
I didn't realize svn delete would delete my local copy, I just wanted it
I didn't find this anywhere - can i tell Play! that a specific controller
I didn't expect this is that complex. But I can't figure out how I

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.