views:

57

answers:

2

Hi all!

I have a list of 55 (or any number but 55 at the moment) questions in my iPhone app.

I have written it at the moment so it goes through the questions from 1 to 55 in number order.

However, I would like to make this order random (or pseudo-random anyway).

I can do it programatically by generating a random number to pick one of the questions and then creating a second list of numbers and checking each time that I haven't already got it before putting that question in the list and picking a new random question.

I would like to know if there is a better/easier way of doing this though?

Like it's possible to sort lists by numerical or alphabetical order using functions, is it possible to sort them randomly and what type of list should I use?

Any help is appreciated.

Thanks

Oliver

A: 

Use a NSMutableArray, send it a sortUsingSelector: message. In the selector, return NSOrderedAscending and NSOrderedDescending randomly.

tob
Thanks, will have a look at the results at home tonight for this.
Fogmeister
+2  A: 

Fisher–Yates shuffle

Ani
Love that algorithm.Makes light work of the random sorting. Esp the "modern version" if tob's solution doesn't work I'll def write this, even just to say I've used it :DThanks
Fogmeister
Works perfectly in 4 lines of code :DThanks!
Fogmeister