0

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

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • 1
    possible duplicate of [canonical way to randomize an NSArray in Objective C](http://stackoverflow.com/questions/791232/canonical-way-to-randomize-an-nsarray-in-objective-c) – kennytm Aug 17 '10 at 14:41
  • possibly, thanks for the link. I did search before posting but didn't come across anything. Thanks – Fogmeister Aug 17 '10 at 14:59

2 Answers2

2

Fisher–Yates shuffle

Ani
  • 111,048
  • 26
  • 262
  • 307
  • 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 :D Thanks – Fogmeister Aug 17 '10 at 14:44
0

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

tobiasbayer
  • 10,269
  • 4
  • 46
  • 64