-3

Possible Duplicate:
What's the Best Way to Shuffle an NSMutableArray?

iam developing one application.In that i have one array.That contain 5 values 1,2,3,4,5.ANd i want to display that values in random manner.For example first time it display 3 2 4 5 1.And second time 1 3 2 4 5.Like this every time i want to change the order.So please tell me how to do this one.

Community
  • 1
  • 1
Venkat1282
  • 113
  • 1
  • 6
  • 2
    duplicate : http://stackoverflow.com/questions/7047085/reading-random-values-from-an-array, http://stackoverflow.com/questions/56648/whats-the-best-way-to-shuffle-an-nsmutablearray, http://stackoverflow.com/questions/4202102/iphone-nsarray-nsmutablearray-re-arrange-in-random-order – Yama Apr 04 '12 at 04:28
  • 1
    Please learn to search. A simple lookup of "random nsarray" would yield you many results. – sudo rm -rf Apr 04 '12 at 04:30
  • use rand(), random() or arc4random() function – Hiren Apr 04 '12 at 04:36
  • 1
    answers are everywhere, just learn to find them :) – Bazinga Apr 04 '12 at 07:31

1 Answers1

1
  1. you can generate a random number between 0 and array size -1
  2. use the number to index the array
  3. remove item at the array index

sample code:

int i = arc4random() % 4;
NSString value = [arr objectAtIndex: i]
[arr removeObjectAtIndex: i];

repeat the steps till you have an empty array.

tangqiaoboy
  • 1,476
  • 1
  • 15
  • 32
Charlie Wu
  • 7,657
  • 5
  • 33
  • 40