Advertise here




Advertise here

Howdy, Stranger!

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

Array Shuffle?

thisismyurlthisismyurl Posts: 5New Users
edited November 2011 in iPhone SDK Tools & Utilities
Hi all, sorry if this is a painfully simple question but ...

in PHP we have a command called shuffle() which randomizes the order of an array (ie shuffle($myArray)).

Is there something similar on the iPhone?


--
Christopher Ross (complete iPhone noob)
Christopher Ross :: Technology Evangelism, Web Marketing and Internet Consulting
Post edited by thisismyurl on
· ·

Replies

  • gaoning87gaoning87 Posts: 23Registered Users
    edited June 2009
    Hi all, sorry if this is a painfully simple question but ...

    in PHP we have a command called shuffle() which randomizes the order of an array (ie shuffle($myArray)).

    Is there something similar on the iPhone?


    --
    Christopher Ross (complete iPhone noob)
    Christopher Ross :: Technology Evangelism, Web Marketing and Internet Consulting

    nope,there isn't offcal function for shuffling array.Tell you some:
    1.random a number or something by including math.h and another header(I forgot,you can google)
    2.call a for loop,push every number you randomed into your array
    what do you think about?
    send me a feedback
    · ·
  • AvocadoAvocado Posts: 20Registered Users
    edited June 2009
    Another approach, using an NSMutableArray:

    #import "time.h"

    ...

    srand( time(NULL) );

    int randomIndex;

    for( int index = 0; index < numItems; index++ )
    {
    randomIndex= rand() % numItems + 1;

    [theArray exchangeObjectAtIndex:index withObjectAtIndex:randomIndex];
    }

    Hopefully that works.
    · ·
  • kalxkalx Posts: 91Registered Users @
    edited June 2009
    Avocado wrote: »
    Another approach, using an NSMutableArray:

    #import "time.h"

    ...

    srand( time(NULL) );

    int randomIndex;

    for( int index = 0; index < numItems; index++ )
    {
    randomIndex= rand() % numItems + 1;

    [theArray exchangeObjectAtIndex:index withObjectAtIndex:randomIndex];
    }

    Hopefully that works.

    That should be a good solution for the OP, except I don't think you want to add 1 to the random index.

    randomIndex= rand() % numItems + 1;

    If your numItems is 5, you want the randomIndex to be a value from 0 through 4, but by adding 1 you're effectively making it 1 through 5 instead.

    - kalx
    · ·
  • AvocadoAvocado Posts: 20Registered Users
    edited June 2009
    kalx wrote: »
    That should be a good solution for the OP, except I don't think you want to add 1 to the random index.

    randomIndex= rand() % numItems + 1;

    If your numItems is 5, you want the randomIndex to be a value from 0 through 4, but by adding 1 you're effectively making it 1 through 5 instead.

    - kalx

    Absolutely right, nice catch.
    · ·
  • thisismyurlthisismyurl Posts: 5New Users
    edited June 2009
    @all, thanks for the reply and support.

    I'm a complete noob at this and don't want to trouble you guys with every question, but I really really appreciate this piece of code! Thank you.

    Chris.
    · ·
  • gaoning87gaoning87 Posts: 23Registered Users
    edited June 2009
    @all, thanks for the reply and support.

    I'm a complete noob at this and don't want to trouble you guys with every question, but I really really appreciate this piece of code! Thank you.

    Chris.

    um,you can question me every time.
    my mail:gaoning87@163.com
    · ·
  • MikesTooLzMikesTooLz Posts: 211Registered Users
    edited June 2009
    I just used this in my bingo app to shuffle what the game squares say.
    Thanks
    <a href="http://twitter.com/MikesTooLz" target="_blank">twitter.jpg</a>
    · ·
  • sherwood87sherwood87 Posts: 3New Users
    edited October 2011
    //Use a trick to "sort" the array into random order.
    //this trick involves using a sort function "randomize" that puts
    //the items in random order.
    [randomArray sortUsingFunction: randomize context:NULL];
    · ·
  • Oliver DrobnikOliver Drobnik Vienna, AustriaPosts: 538New Users, Registered Users @ @ @
    edited November 2011
    Buy my book "Barcodes with iOS 7": http://bitly.com/barcodes_iOS

    Get 50% until March 9th with promo code "mldrobnik"
    · ·
Sign In or Register to comment.